@import url("https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;700&family=Amiri:ital,wght@0,400;0,700;1,400&display=swap");

/* 1. Design tokens ------------------------------------------------ */
.kh-app {
  --kh-bg:        #f6f0e3;
  --kh-surface:   #fffaf0;
  --kh-text:      #2a2118;
  --kh-muted:     #7a6a55;
  --kh-accent:    #1f6f78;
  --kh-gold:      #a8741a;
  --kh-danger:    #b3392f;
  --kh-border:    #e2d6bd;
  --kh-radius:    10px;
  --kh-shadow:    0 2px 14px rgba(60, 40, 10, .12);
  --kh-transition: .2s ease;
  --kh-poem-size: clamp(1.35rem, 3.6vw, 2rem);
  --kh-poem-font: 'Vazirmatn', 'Amiri', 'Noto Naskh Arabic', Tahoma, serif;
  --kh-fr-font:   'Playfair Display', Georgia, 'Times New Roman', serif;
}

/* 2. Thèmes -------------------------------------------------------- */
.kh-app[data-theme="dark"] {
  --kh-bg: #14181c; --kh-surface: #1c2227; --kh-text: #ece6d8; --kh-muted: #a09884;
  --kh-accent: #63c0c9; --kh-gold: #e0b45a; --kh-danger: #ef7d72; --kh-border: #303840;
  --kh-shadow: 0 2px 18px rgba(0, 0, 0, .5);
}
@media (prefers-color-scheme: dark) {
  .kh-app[data-theme="auto"] {
    --kh-bg: #14181c; --kh-surface: #1c2227; --kh-text: #ece6d8; --kh-muted: #a09884;
    --kh-accent: #63c0c9; --kh-gold: #e0b45a; --kh-danger: #ef7d72; --kh-border: #303840;
    --kh-shadow: 0 2px 18px rgba(0, 0, 0, .5);
  }
}

/* 3. Layout principal ---------------------------------------------- */
.kh-app {
  position: relative;
  max-width: 880px;
  margin: 0 auto;
  padding: 1rem;
  min-height: 70vh;
  background: var(--kh-bg);
  color: var(--kh-text);
  border-radius: var(--kh-radius);
  display: flex;
  flex-direction: column;
  gap: 1rem;
  font-family: 'Source Sans 3', 'Segoe UI', system-ui, sans-serif;
}
.kh-app button, .kh-app input { font: inherit; color: inherit; }
.kh-app :focus-visible { outline: 2px solid var(--kh-accent); outline-offset: 2px; }

/* 4. Barre d'outils ------------------------------------------------- */
.kh-bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: .6rem;
  padding: .5rem 0 .8rem; border-bottom: 1px solid var(--kh-border);
  position: sticky; top: var(--kh-top, 0); z-index: 160; background: var(--kh-bg);   /* reste visible en défilement */
}
.kh-title {
  flex: 1 1 14rem; margin: 0; text-align: center;
  font-family: 'Playfair Display', Georgia, serif; font-weight: 600;
  font-size: clamp(1.05rem, 2.6vw, 1.5rem); letter-spacing: -.01em;
}
.kh-title small {
  display: block; margin-top: .15rem; font-family: var(--kh-poem-font);
  font-weight: 400; font-size: .85em; color: var(--kh-gold);
}
.kh-bar-tools { display: flex; gap: .35rem; flex-wrap: wrap; }

.kh-lang { display: flex; gap: .25rem; }
.kh-lang-opt { padding: .4rem .75rem; }

/* Paramètres d'affichage */
.kh-settings {
  position: absolute; z-index: 150; top: calc(100% + .4rem); inset-inline-end: 0; width: min(21rem, 100%);
  background: var(--kh-surface); border: 1px solid var(--kh-border); border-radius: var(--kh-radius);
  box-shadow: var(--kh-shadow); padding: .8rem 1rem; display: flex; flex-direction: column; gap: .8rem;
}
.kh-settings[hidden] { display: none; }
.kh-set-row { display: flex; align-items: center; justify-content: space-between; gap: .8rem; flex-wrap: wrap; }
.kh-set-label { font-size: .85rem; font-weight: 600; color: var(--kh-muted); }
.kh-set-ctrl { display: flex; align-items: center; gap: .3rem; }
.kh-set-val { min-width: 3.2rem; text-align: center; font-size: .85rem; }
.kh-theme-opt { padding: .35rem .7rem; font-size: .85rem; }

/* 5. Zone lecture --------------------------------------------------- */
.kh-reader { display: flex; flex-direction: column; align-items: center; gap: .8rem; }
.kh-chapter {
  margin: 0; font-size: .75rem; font-weight: 600; text-transform: uppercase;
  letter-spacing: .12em; color: var(--kh-accent); text-align: center;
}
.kh-chapter [lang="fa"] { text-transform: none; letter-spacing: 0; font-family: var(--kh-poem-font); font-size: 1.05em; margin-inline-start: .5em; }
.kh-card {
  width: 100%; background: var(--kh-surface); border: 1px solid var(--kh-border);
  border-radius: var(--kh-radius); box-shadow: var(--kh-shadow);
  padding: clamp(1.2rem, 4vw, 2.6rem) clamp(1rem, 4vw, 2.4rem);
  position: relative; min-height: 16rem;
  display: flex; flex-direction: column; justify-content: center;
}
.kh-card-head {
  position: absolute; top: .7rem; inset-inline: 1rem; display: flex; justify-content: space-between;
  color: var(--kh-muted); font-size: .85rem;
}
.kh-num { font-weight: 600; color: var(--kh-gold); }
.kh-num [lang="fa"] { margin-inline-start: .5em; font-family: var(--kh-poem-font); font-weight: 400; }
.kh-star { color: var(--kh-gold); cursor: help; }

/* 6. Poème ----------------------------------------------------------- */
.kh-poem {
  font-family: var(--kh-poem-font); font-size: var(--kh-poem-size);
  line-height: 2.1; text-align: center; margin: 1rem 0 .4rem;
}
.kh-poem .kh-v { display: block; }
.kh-poem .kh-v:nth-child(2n) { margin-bottom: .6rem; }
.kh-poem.kh-classic {
  display: grid; grid-template-columns: 1fr 1fr;
  column-gap: 1.6rem; max-width: none; text-align: start;
  font-size: calc(var(--kh-poem-size) * .82); line-height: 2.2;
}
.kh-poem.kh-classic .kh-v:nth-child(2n) { margin-bottom: 0; text-align: end; }
.kh-poem.kh-classic .kh-v:nth-child(2n+1) { text-align: start; }

.kh-poem.kh-fr { font-family: var(--kh-fr-font); font-size: calc(var(--kh-poem-size) * .8); line-height: 1.8; font-style: italic; }
.kh-card-title {
  margin: 1.2rem 0 0; text-align: center; font-family: var(--kh-fr-font); font-size: .95rem;
  font-weight: 600; letter-spacing: .06em; color: var(--kh-gold);
}
.kh-card-title[hidden], .kh-note[hidden] { display: none; }
.kh-card-title + .kh-poem { margin-top: .3rem; }
.kh-note { margin: .4rem 0 0; text-align: center; font-size: .8rem; color: var(--kh-muted); }
.kh-card.kh-enter { animation: kh-fade .28s ease both; }
@keyframes kh-fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* 7. Actions & mode défilement --------------------------------------- */
.kh-actions { display: flex; gap: .5rem; flex-wrap: wrap; justify-content: center; }
.kh-scroll { display: flex; flex-direction: column; gap: 1rem; }
.kh-scroll .kh-card { min-height: 0; padding-top: 2.4rem; scroll-margin-top: 5rem; }
.kh-scroll .kh-chapter { margin-top: .8rem; font-size: .85rem; }
.kh-scroll-fav { position: absolute; bottom: .6rem; inset-inline-start: 1rem; }
.kh-scroll-tts { position: absolute; bottom: .6rem; inset-inline-start: 4rem; }

/* 8. Navigation ------------------------------------------------------- */
.kh-nav { direction: rtl; display: flex; align-items: center; gap: .6rem; position: sticky; bottom: 0; padding: .6rem 0; background: var(--kh-bg); }
.kh-nav .kh-btn { direction: ltr; }
.kh-progress { flex: 1; display: flex; flex-direction: column; align-items: center; gap: .15rem; min-width: 0; }
.kh-progress input[type="range"] { width: 100%; accent-color: var(--kh-accent); direction: rtl; }
.kh-app:not([data-lang="fa"]) .kh-nav { direction: ltr; }
.kh-app:not([data-lang="fa"]) .kh-progress input[type="range"] { direction: ltr; }
.kh-count { direction: ltr; unicode-bidi: isolate; font-size: .8rem; color: var(--kh-muted); }
.kh-credit { margin: 0; font-size: .8rem; font-weight: 300; color: var(--kh-muted); text-align: center; }

/* 9. Composants : boutons, sommaire, toast ---------------------------- */
.kh-btn {
  display: inline-flex; align-items: center; gap: .35rem; cursor: pointer;
  background: var(--kh-surface); border: 1px solid var(--kh-border); border-radius: 999px;
  padding: .45rem .9rem; font-size: .9rem; transition: background var(--kh-transition), border-color var(--kh-transition);
}
.kh-btn:hover { border-color: var(--kh-accent); }
.kh-btn--icon { padding: .45rem .7rem; }
.kh-btn--nav { background: var(--kh-accent); border-color: var(--kh-accent); color: #fff; }
.kh-app[data-theme="dark"] .kh-btn--nav { color: #0b1114; }
.kh-btn[aria-pressed="true"] { border-color: var(--kh-gold); color: var(--kh-gold); }
.kh-btn:disabled { opacity: .4; cursor: default; }
.kh-btn.is-fav { color: var(--kh-danger); border-color: var(--kh-danger); }

.kh-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 200; }
.kh-toc {
  position: fixed; top: 0; bottom: 0; inset-inline-start: 0; width: min(22rem, 92vw); z-index: 210;
  background: var(--kh-surface); color: var(--kh-text); border-inline-end: 1px solid var(--kh-border);
  box-shadow: var(--kh-shadow); display: flex; flex-direction: column;
}
.kh-toc[hidden], .kh-backdrop[hidden], .kh-reader[hidden], .kh-scroll[hidden], .kh-error[hidden] { display: none; }
.kh-toc-head { display: flex; justify-content: space-between; align-items: center; padding: .8rem 1rem; border-bottom: 1px solid var(--kh-border); }
.kh-toc-head h2 { margin: 0; font-family: 'Playfair Display', Georgia, serif; font-size: 1.3rem; }
.kh-toc-tabs { display: flex; border-bottom: 1px solid var(--kh-border); }
.kh-tab { flex: 1; padding: .6rem; background: none; border: 0; border-bottom: 2px solid transparent; cursor: pointer; color: var(--kh-muted); }
.kh-tab[aria-selected="true"] { color: var(--kh-text); border-bottom-color: var(--kh-gold); font-weight: 600; }
.kh-toc-body { overflow-y: auto; padding: .6rem 1rem 1.2rem; }
.kh-toc-sec { margin: 1rem 0 .4rem; font-size: .8rem; color: var(--kh-accent); font-weight: 600; text-transform: uppercase; letter-spacing: .08em; }
.kh-toc-sec [lang="fa"] { font-family: var(--kh-poem-font); text-transform: none; letter-spacing: 0; font-size: 1.1em; margin-inline-start: .4em; }
.kh-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(2.6rem, 1fr)); gap: .3rem; }
.kh-cell {
  padding: .35rem 0; text-align: center; cursor: pointer; border: 1px solid var(--kh-border);
  border-radius: 6px; background: transparent; font-size: .85rem;
}
.kh-cell:hover { border-color: var(--kh-accent); }
.kh-cell.is-current { background: var(--kh-accent); color: #fff; border-color: var(--kh-accent); }
.kh-cell.is-fav::after { content: '♥'; color: var(--kh-danger); font-size: .6em; margin-inline-start: .15em; }
.kh-fav-item { display: block; width: 100%; text-align: start; padding: .6rem 0; border: 0; border-bottom: 1px solid var(--kh-border); background: none; cursor: pointer; }
.kh-fav-item span { display: block; font-family: var(--kh-poem-font); direction: rtl; text-align: right; }
.kh-fav-item span[lang="fr"] { direction: ltr; text-align: left; font-family: var(--kh-fr-font); font-style: italic; }
.kh-fav-item small { color: var(--kh-muted); }
.kh-empty { color: var(--kh-muted); font-size: .9rem; }

.kh-toast {
  position: fixed; bottom: 4.5rem; left: 50%; transform: translateX(-50%); z-index: 300;
  background: var(--kh-text); color: var(--kh-bg); padding: .5rem 1rem; border-radius: 999px;
  font-size: .9rem; opacity: 0; pointer-events: none; transition: opacity var(--kh-transition);
}
.kh-toast.is-on { opacity: 1; }
.kh-error { margin: 0; padding: .7rem 1rem; border: 1px solid var(--kh-danger); border-radius: var(--kh-radius); color: var(--kh-danger); }

/* 9b. Planche mémo ---------------------------------------------------- */
.kh-btn[hidden] { display: none; }
.kh-memo { position: fixed; inset: 0; z-index: 400; display: flex; align-items: center; justify-content: center; padding: .6rem; }
.kh-memo[hidden] { display: none; }
.kh-memo-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .7); }
.kh-memo-box {
  position: relative; display: flex; flex-direction: column; gap: .5rem; max-width: min(100%, 44rem); max-height: 100%;
  background: var(--kh-surface); color: var(--kh-text); border: 1px solid var(--kh-border);
  border-radius: var(--kh-radius); box-shadow: var(--kh-shadow); padding: .7rem;
}
.kh-memo-head, .kh-memo-nav { display: flex; align-items: center; justify-content: space-between; gap: .6rem; }
.kh-memo-title { margin: 0; font-family: 'Playfair Display', Georgia, serif; font-size: 1.05rem; }
.kh-memo-img { display: block; min-height: 0; max-width: 100%; max-height: calc(100vh - 9rem); object-fit: contain; margin: 0 auto; border-radius: 6px; }

/* 10. Responsive & préférences --------------------------------------- */
/* Aide (bouton ?) */
.kh-help { position: fixed; inset: 0; z-index: 450; display: flex; align-items: center; justify-content: center; padding: .6rem; }
.kh-help[hidden] { display: none; }
.kh-help-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .6); }
.kh-help-box {
  position: relative; display: flex; flex-direction: column; gap: .5rem; width: min(100%, 42rem); max-height: 100%;
  background: var(--kh-surface); color: var(--kh-text); border: 1px solid var(--kh-border);
  border-radius: var(--kh-radius); box-shadow: var(--kh-shadow); padding: .8rem 1rem;
}
.kh-help-head { display: flex; align-items: center; justify-content: space-between; gap: .6rem; }
.kh-help-title { margin: 0; font-family: var(--kh-fr-font); font-size: 1.15rem; }
.kh-help-body { overflow-y: auto; padding-inline-end: .4rem; line-height: 1.55; font-size: .95rem; }
.kh-help-body:focus-visible { outline: 2px solid var(--kh-accent); outline-offset: 2px; }
.kh-help-body h3 { margin: 1.1rem 0 .3rem; font-size: 1rem; color: var(--kh-accent); }
.kh-help-body section:first-child h3 { margin-top: 0; }
.kh-help-body p { margin: .35rem 0; }
.kh-help-body ul { margin: .3rem 0; padding-inline-start: 1.2rem; }
.kh-help-body li { margin: .25rem 0; }
.kh-help-body kbd {
  display: inline-block; min-width: 1.4em; padding: .05em .4em; text-align: center; font: inherit; font-size: .85em;
  background: var(--kh-bg); border: 1px solid var(--kh-border); border-bottom-width: 2px; border-radius: 5px;
}
.kh-help-body code { font-size: .9em; background: var(--kh-bg); padding: .05em .3em; border-radius: 4px; }
.kh-keys { margin: .4rem 0; display: grid; gap: .3rem; }
.kh-keys > div { display: grid; grid-template-columns: 7rem 1fr; gap: .6rem; align-items: baseline; }
.kh-keys dt, .kh-keys dd { margin: 0; }

@media (max-width: 600px) {
  .kh-keys > div { grid-template-columns: 1fr; gap: 0; }
  .kh-app { padding: .7rem; border-radius: 0; }
  .kh-btn span { font-size: .85rem; }
  .kh-btn--nav span { display: none; }
  .kh-poem.kh-classic { grid-template-columns: 1fr; }
  .kh-poem.kh-classic .kh-v:nth-child(n) { text-align: center; }
  .kh-poem.kh-classic .kh-v:nth-child(2n) { margin-bottom: .6rem; }
}
@media (prefers-reduced-motion: reduce) {
  .kh-card.kh-enter { animation: none; }
  .kh-toast, .kh-btn { transition: none; }
}
