:root {
  --ink: #0f2a31;
  --ink-soft: #3d5c66;
  --paper: #f3f7f8;
  --teal: #1a5c6b;
  --teal-deep: #0e3d48;
  --teal-bright: #2a8a9e;
  --line: rgba(15, 42, 49, 0.1);
  --shadow: 0 24px 60px rgba(14, 61, 72, 0.22);
  --radius: 18px;
  --font: "Vazirmatn", system-ui, sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

*,
*::before,
*::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  max-width: 100%;
  overflow-x: hidden;
}

html, body {
  margin: 0;
  min-height: 100%;
  max-width: 100%;
  font-family: var(--font);
  color: var(--ink);
  background: var(--paper);
}

body {
  overflow-x: hidden;
  padding-bottom: var(--safe-bottom);
  width: 100%;
}

.atmosphere {
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(1200px 700px at 100% -10%, rgba(42, 138, 158, 0.28), transparent 55%),
    radial-gradient(900px 600px at -10% 30%, rgba(26, 92, 107, 0.18), transparent 50%),
    linear-gradient(165deg, #f7fbfc 0%, #e8f2f4 45%, #dce9ed 100%);
}

.atmosphere::after {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0.35;
  background-image:
    linear-gradient(rgba(26, 92, 107, 0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(26, 92, 107, 0.04) 1px, transparent 1px);
  background-size: 28px 28px;
  mask-image: linear-gradient(180deg, #000 0%, transparent 85%);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 0.85rem;
  padding:
    calc(0.85rem + var(--safe-top))
    calc(1rem + var(--safe-right))
    0.85rem
    calc(1rem + var(--safe-left));
  backdrop-filter: blur(14px);
  background: rgba(243, 247, 248, 0.9);
  border-bottom: 1px solid var(--line);
  max-width: 100%;
  min-width: 0;
  overflow: hidden;
}

.brand-block {
  min-width: 0;
  flex: 1 1 auto;
}

.brand {
  margin: 0;
  font-size: clamp(1.2rem, 4.8vw, 2rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.3;
  color: var(--teal-deep);
}

.subtitle {
  margin: 0.25rem 0 0;
  color: var(--ink-soft);
  font-size: clamp(0.8rem, 3.2vw, 0.9rem);
  font-weight: 500;
  line-height: 1.4;
}

.stats {
  flex-shrink: 0;
  padding: 0.4rem 0.75rem;
  border-radius: 999px;
  background: rgba(26, 92, 107, 0.1);
  color: var(--teal);
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
}

.shell {
  width: min(920px, calc(100% - 1.25rem));
  max-width: 100%;
  margin: 0.85rem auto 2rem;
  display: grid;
  gap: 0.85rem;
  min-width: 0;
}

.toolbar {
  padding: 0.95rem 1rem;
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.78);
  border: 1px solid var(--line);
  box-shadow: 0 10px 30px rgba(14, 61, 72, 0.06);
  animation: rise 0.55s ease both;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
}

.jump {
  display: grid;
  gap: 0.4rem;
  font-weight: 600;
  color: var(--teal-deep);
  font-size: 0.95rem;
}

.jump-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.55rem;
}

input[type="search"] {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.85rem 1rem;
  font: inherit;
  font-size: 16px; /* prevent iOS zoom */
  background: #fff;
  color: var(--ink);
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  -webkit-appearance: none;
  appearance: none;
}

input[type="search"]:focus {
  border-color: var(--teal-bright);
  box-shadow: 0 0 0 4px rgba(42, 138, 158, 0.15);
}

.letter-bar {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.35rem;
  margin-top: 0.85rem;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding-bottom: 0.2rem;
  max-width: 100%;
  min-width: 0;
  width: 100%;
}

.letter-bar::-webkit-scrollbar { display: none; }

.letter-chip {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--teal-deep);
  border-radius: 10px;
  min-width: 2.35rem;
  min-height: 2.35rem;
  padding: 0 0.5rem;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  flex: 0 0 auto;
  touch-action: manipulation;
}

.letter-chip.active,
.letter-chip:hover {
  background: rgba(26, 92, 107, 0.12);
  border-color: rgba(26, 92, 107, 0.25);
}

.hint {
  margin: 0.75rem 0 0;
  color: var(--ink-soft);
  font-size: 0.84rem;
  line-height: 1.55;
  overflow-wrap: anywhere;
  word-break: break-word;
  max-width: 100%;
}

.btn {
  appearance: none;
  border: none;
  border-radius: 12px;
  padding: 0.85rem 1.05rem;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.2s, box-shadow 0.2s;
  touch-action: manipulation;
  min-height: 44px;
}

.btn:active { transform: scale(0.98); }

.btn.primary {
  background: linear-gradient(145deg, var(--teal-bright), var(--teal));
  color: #fff;
  box-shadow: 0 8px 20px rgba(26, 92, 107, 0.28);
}

.btn.ghost {
  background: rgba(26, 92, 107, 0.08);
  color: var(--teal-deep);
}

.topic-pager {
  display: flex;
  gap: 0.45rem;
  flex-shrink: 0;
}

.topic-pager .btn {
  padding: 0.45rem 0.75rem;
  min-height: 36px;
  font-size: 0.82rem;
  white-space: nowrap;
}

.list-panel {
  border-radius: calc(var(--radius) + 4px);
  overflow: hidden;
  background: rgba(255, 255, 255, 0.82);
  border: 1px solid var(--line);
  box-shadow: 0 16px 40px rgba(14, 61, 72, 0.08);
  animation: rise 0.7s ease both;
  min-width: 0;
  max-width: 100%;
}

.list-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: linear-gradient(180deg, rgba(26, 92, 107, 0.1), rgba(26, 92, 107, 0.04));
  color: var(--teal-deep);
  font-size: 0.82rem;
  font-weight: 700;
  border-bottom: 1px solid var(--line);
}

.topic-list { display: grid; }

.topic-item {
  border-bottom: 1px solid rgba(15, 42, 49, 0.06);
}

.topic-toggle {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  border: none;
  background: transparent;
  padding: 1rem 1rem;
  min-height: 56px;
  font: inherit;
  text-align: right;
  cursor: pointer;
  touch-action: manipulation;
}

.topic-toggle:hover,
.topic-item.open .topic-toggle {
  background: rgba(42, 138, 158, 0.08);
}

.topic-name {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--ink);
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.topic-count {
  font-size: 0.8rem;
  color: var(--ink-soft);
  font-weight: 600;
  white-space: nowrap;
}

.topic-numbers {
  display: none;
  padding: 0 0.85rem 1rem;
  gap: 0.4rem;
}

.topic-item.open .topic-numbers {
  display: flex;
  flex-wrap: wrap;
}

.num-chip {
  appearance: none;
  border: 1px solid rgba(26, 92, 107, 0.18);
  background: #fff;
  color: var(--teal-deep);
  border-radius: 10px;
  padding: 0.5rem 0.7rem;
  min-height: 40px;
  font: inherit;
  font-size: 0.95rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  transition: background 0.15s, transform 0.15s;
  touch-action: manipulation;
}

.num-chip:hover,
.num-chip:focus-visible {
  background: rgba(26, 92, 107, 0.12);
  outline: none;
}

.num-chip:active { transform: scale(0.96); }

.more-note {
  width: 100%;
  padding-top: 0.25rem;
}

.empty-state {
  margin: 0;
  padding: 2rem 1rem;
  text-align: center;
  color: var(--ink-soft);
}

.modal[hidden] { display: none; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: end center;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(8, 28, 34, 0.55);
  backdrop-filter: blur(4px);
  animation: fade 0.25s ease both;
}

.modal-sheet {
  position: relative;
  z-index: 1;
  width: min(960px, 100%);
  height: min(96dvh, 980px);
  max-height: min(96dvh, 980px);
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  background: linear-gradient(180deg, #f8fcfd 0%, #eef5f7 100%);
  border-radius: 22px 22px 0 0;
  box-shadow: var(--shadow);
  animation: sheet-up 0.35s cubic-bezier(0.22, 1, 0.36, 1) both;
  overflow: hidden;
}

.modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1rem 1rem 0.35rem;
}

.modal-kicker {
  margin: 0;
  color: var(--teal);
  font-size: 0.8rem;
  font-weight: 700;
}

#modalTitle {
  margin: 0.2rem 0 0;
  font-size: clamp(1.1rem, 4vw, 1.55rem);
  font-weight: 800;
  color: var(--teal-deep);
  line-height: 1.3;
}

.icon-btn {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 12px;
  background: rgba(26, 92, 107, 0.1);
  color: var(--teal-deep);
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  touch-action: manipulation;
}

.modal-toolbar {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.4rem;
  padding: 0.45rem 0.85rem 0.7rem;
}

.modal-toolbar .btn {
  padding: 0.55rem 0.35rem;
  font-size: 0.82rem;
  min-height: 42px;
}

.page-stage {
  position: relative;
  margin: 0 0.65rem;
  border-radius: 14px;
  background:
    radial-gradient(circle at 20% 0%, rgba(255, 255, 255, 0.9), transparent 45%),
    linear-gradient(160deg, #102830, #1a5c6b 55%, #0e3d48);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
  overscroll-behavior: contain;
  display: grid;
  place-items: center;
}

#pageCanvas {
  display: block;
  margin: 0;
  max-width: none;
  border-radius: 4px;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.35);
  background: #fff;
}

.page-loader {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: rgba(255, 255, 255, 0.9);
  font-weight: 600;
  pointer-events: none;
  padding: 1rem;
  text-align: center;
}

.page-loader[hidden] { display: none; }

.modal-foot {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.55rem;
  padding: 0.75rem 0.85rem calc(0.75rem + var(--safe-bottom));
  background: rgba(255, 255, 255, 0.78);
  border-top: 1px solid var(--line);
}

@keyframes rise {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: none; }
}

@keyframes fade {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes sheet-up {
  from { opacity: 0; transform: translateY(28px); }
  to { opacity: 1; transform: none; }
}

@media (min-width: 800px) {
  .modal {
    place-items: center;
    padding: 1.5rem;
  }
  .modal-sheet {
    border-radius: 22px;
    height: min(90vh, 920px);
    max-height: min(90vh, 920px);
  }
  .letter-bar {
    flex-wrap: wrap;
    overflow: visible;
  }
}

@media (max-width: 560px) {
  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 0.55rem;
  }

  .brand-block {
    min-width: 0;
    max-width: 100%;
  }

  .brand,
  .subtitle {
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  .stats {
    align-self: flex-start;
    max-width: 100%;
    white-space: normal;
  }

  .shell {
    width: calc(100% - 0.85rem);
    max-width: calc(100% - 0.85rem);
    margin-top: 0.65rem;
  }

  .toolbar,
  .list-panel {
    border-radius: 14px;
  }

  .jump-row {
    grid-template-columns: 1fr;
  }

  .jump-row .btn {
    width: 100%;
  }

  .list-head {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .topic-name {
    font-size: 1rem;
  }

  .modal-toolbar {
    grid-template-columns: 1fr 1fr;
  }

  .modal-sheet {
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 16px 16px 0 0;
  }

  .page-stage {
    margin: 0 0.45rem;
    border-radius: 10px;
  }

  .num-chip {
    min-width: 3.1rem;
  }
}

@media (max-width: 380px) {
  .brand {
    font-size: 1.12rem;
  }

  .modal-toolbar .btn {
    font-size: 0.75rem;
    padding: 0.5rem 0.25rem;
  }
}
