/* ---------------------------------------------------------------
   Nepali Readability Scorer — design tokens
   Palette grounded in manuscript/ink concept: "graphemic density"
   reads naturally as ink density on parchment. Band colours run
   cool -> warm as difficulty rises (teal -> indigo -> marigold -> crimson).
------------------------------------------------------------------*/
:root {
  --paper: #f3f0e4;
  --paper-raised: #faf8f0;
  --ink: #23201b;
  --ink-soft: #5c564a;
  --rule: #d8d2bf;
  --rule-strong: #b9b29b;

  --band-primary: #2f5d62;   /* Early Primary   0-30  */
  --band-middle: #3b4f72;    /* Middle School   31-60 */
  --band-secondary: #d98e2b; /* Secondary       61-80 */
  --band-advanced: #a83a32;  /* Higher Sec/Adv  81-100*/

  --focus: #3b4f72;

  --font-display: "Fraunces", "Georgia", serif;
  --font-devanagari: "Hind", "Noto Sans Devanagari", sans-serif;
  --font-ui: "Work Sans", "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body { min-width: 320px; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-ui);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--band-middle); }

::selection { background: var(--band-secondary); color: var(--paper-raised); }

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* ---------------- layout shell ---------------- */

.shell {
  max-width: 1180px;
  margin: 0 auto;
  padding: clamp(1.25rem, 4vw, 2.5rem) clamp(1rem, 4vw, 1.75rem) 4rem;
}

header.masthead {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.75rem 1.25rem;
  border-bottom: 1px solid var(--rule-strong);
  padding-bottom: 1.5rem;
  margin-bottom: 2.25rem;
}

.masthead h1 {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(2rem, 3.4vw, 2.75rem);
  margin: 0;
  letter-spacing: -0.01em;
}

.masthead .devanagari-title {
  font-family: var(--font-devanagari);
  font-weight: 600;
  margin-right: 0.6rem;
}

.masthead p {
  margin: 0;
  color: var(--ink-soft);
  font-size: 0.98rem;
  max-width: 46ch;
  flex: 1 1 20rem;
}

.wordlist-chip {
  margin-left: auto;
  align-self: center;
  font-size: 0.82rem;
  color: var(--ink-soft);
  border: 1px solid var(--rule-strong);
  border-radius: 3px;
  padding: 0.3rem 0.65rem;
  max-width: 100%;
  overflow-wrap: anywhere;
}

/* ---------------- two-zone grid ---------------- */

.workspace {
  display: grid;
  grid-template-columns: minmax(280px, 380px) 1fr;
  gap: 2.25rem;
  align-items: start;
}

.workspace > * { min-width: 0; }

@media (max-width: 880px) {
  .workspace { grid-template-columns: 1fr; }
}

.panel {
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: 4px;
  padding: clamp(1rem, 3vw, 1.5rem);
}

.input-panel { position: sticky; top: 1.5rem; }
@media (max-width: 880px) { .input-panel { position: static; } }

.panel h2 {
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 600;
  margin: 0 0 0.9rem;
}

textarea#passage {
  width: 100%;
  min-height: 190px;
  resize: vertical;
  font-family: var(--font-devanagari);
  font-size: 1.12rem;
  line-height: 1.85;
  padding: 0.85rem;
  border: 1px solid var(--rule-strong);
  border-radius: 3px;
  background: #fff;
  color: var(--ink);
}

.example-row {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.6rem;
  flex-wrap: wrap;
}

button, .btn {
  font-family: var(--font-ui);
  font-size: 0.88rem;
  cursor: pointer;
  border-radius: 3px;
  border: 1px solid var(--rule-strong);
  background: var(--paper-raised);
  color: var(--ink);
  padding: 0.45rem 0.8rem;
  transition: background-color 120ms ease, border-color 120ms ease;
  min-height: 2.5rem;
}

button:hover { border-color: var(--ink-soft); }

.btn-primary {
  background: var(--band-middle);
  border-color: var(--band-middle);
  color: #fff;
  font-weight: 600;
  padding: 0.7rem 1.1rem;
  font-size: 0.95rem;
  width: 100%;
  margin-top: 1.1rem;
}
.btn-primary:hover { background: #2e3d59; border-color: #2e3d59; }
.btn-primary:disabled { opacity: 0.55; cursor: progress; }

/* ---------------- advanced settings (details) ---------------- */

details.advanced {
  margin-top: 1.25rem;
  border-top: 1px solid var(--rule);
  padding-top: 0.9rem;
}

details.advanced summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 0.92rem;
  list-style: none;
}
details.advanced summary::-webkit-details-marker { display: none; }
details.advanced summary::before {
  content: "▸ ";
  color: var(--ink-soft);
}
details.advanced[open] summary::before { content: "▾ "; }

.weight-group { margin-top: 1rem; }
.weight-group h3 {
  font-size: 0.78rem;
  text-transform: none;
  color: var(--ink-soft);
  margin: 0 0 0.5rem;
  font-weight: 600;
}

.slider-row {
  display: grid;
  grid-template-columns: 5.2rem 1fr 2.6rem;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.55rem;
  font-size: 0.85rem;
}

.slider-row input[type="range"] { width: 100%; accent-color: var(--band-middle); }
.slider-row .val { text-align: right; color: var(--ink-soft); font-variant-numeric: tabular-nums; }

.field-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.6rem;
  margin-top: 0.7rem;
  font-size: 0.85rem;
}
.field-row input[type="number"] {
  width: 4.5rem;
  border: 1px solid var(--rule-strong);
  border-radius: 3px;
  padding: 0.25rem 0.4rem;
  font-family: var(--font-ui);
}

.hint { font-size: 0.78rem; color: var(--ink-soft); margin-top: 0.4rem; }

/* ---------------- results canvas ---------------- */

#results { display: none; }
#results.visible { display: block; }

#error-banner {
  display: none;
  background: #f6e3df;
  border: 1px solid var(--band-advanced);
  color: #6f241d;
  padding: 0.7rem 0.9rem;
  border-radius: 3px;
  margin-bottom: 1.25rem;
  font-size: 0.9rem;
}
#error-banner.visible { display: block; }

.hero {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 1.75rem;
  align-items: center;
  margin-bottom: 1.75rem;
}
@media (max-width: 620px) { .hero { grid-template-columns: 1fr; } }

.hero .score-block { text-align: left; }
.hero .cdrs-number {
  font-family: var(--font-display);
  font-size: clamp(3.2rem, 8vw, 4.6rem);
  font-weight: 600;
  line-height: 1;
  margin: 0;
}
.hero .band-label {
  font-weight: 600;
  font-size: 1.05rem;
  margin-top: 0.35rem;
}
.hero .band-grades { color: var(--ink-soft); font-size: 0.9rem; }

.scale {
  position: relative;
}
.scale-track {
  display: flex;
  height: 0.85rem;
  border-radius: 3px;
  overflow: hidden;
  border: 1px solid var(--rule-strong);
}
.scale-track span { display: block; height: 100%; }
.scale-seg-primary { background: var(--band-primary); flex: 30; }
.scale-seg-middle { background: var(--band-middle); flex: 30; }
.scale-seg-secondary { background: var(--band-secondary); flex: 20; }
.scale-seg-advanced { background: var(--band-advanced); flex: 20; }

.scale-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.72rem;
  color: var(--ink-soft);
  margin-top: 0.3rem;
}

.scale-marker {
  position: absolute;
  top: -0.55rem;
  width: 2px;
  height: 1.95rem;
  background: var(--ink);
  transform: translateX(-1px);
}
.scale-marker::after {
  content: "";
  position: absolute;
  top: -5px;
  left: -4px;
  width: 10px;
  height: 10px;
  background: var(--ink);
  border-radius: 50%;
}

/* metric cards */
.metric-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  margin-bottom: 1.75rem;
}
@media (max-width: 620px) { .metric-row { grid-template-columns: 1fr; } }

.metric-card {
  border: 1px solid var(--rule);
  border-radius: 4px;
  padding: 1rem 1.1rem;
  background: var(--paper-raised);
}
.metric-card .metric-label {
  font-size: 0.8rem;
  color: var(--ink-soft);
  margin-bottom: 0.25rem;
}
.metric-card .metric-value {
  font-family: var(--font-display);
  font-size: 1.9rem;
  font-weight: 600;
}
.metric-card .metric-sub { font-size: 0.78rem; color: var(--ink-soft); margin-top: 0.15rem; }

/* contribution bar */
.contribution {
  margin-bottom: 1.9rem;
}
.contribution h3, .histogram h3, .word-table-wrap h3 {
  font-family: var(--font-display);
  font-size: 1.05rem;
  font-weight: 600;
  margin: 0 0 0.7rem;
}
.contribution-bar {
  display: flex;
  height: 1.9rem;
  border-radius: 3px;
  overflow: hidden;
  border: 1px solid var(--rule-strong);
}
.contribution-bar div { display: flex; align-items: center; justify-content: center; font-size: 0.72rem; color: #fff; }
.contribution-legend {
  display: flex;
  gap: 1.2rem;
  margin-top: 0.55rem;
  font-size: 0.78rem;
  color: var(--ink-soft);
  flex-wrap: wrap;
}
.contribution-legend span.swatch {
  display: inline-block;
  width: 0.65rem; height: 0.65rem;
  border-radius: 2px;
  margin-right: 0.35rem;
  vertical-align: middle;
}

.histogram { margin-bottom: 1.9rem; }
.histogram canvas { display: block; width: 100% !important; height: 220px !important; }

/* word table */
.word-table-wrap { overflow-x: auto; }
table.word-table {
  border-collapse: collapse;
  width: max-content;
  min-width: 100%;
  font-size: 0.85rem;
}
table.word-table th, table.word-table td {
  text-align: left;
  padding: 0.5rem 0.65rem;
  border-bottom: 1px solid var(--rule);
  white-space: nowrap;
}
table.word-table th {
  cursor: pointer;
  color: var(--ink-soft);
  font-weight: 600;
  user-select: none;
  border-bottom: 1px solid var(--rule-strong);
}
table.word-table th.sorted::after { content: " ▾"; }
table.word-table th.sorted.asc::after { content: " ▴"; }
table.word-table td.word-cell { font-family: var(--font-devanagari); font-size: 1rem; }
table.word-table tr.hard td { background: #f6e9e7; }
table.word-table tr.hard td.word-cell { color: #6f241d; font-weight: 600; }
.wordlist-flag { color: var(--ink-soft); font-size: 0.72rem; }

.placeholder {
  color: var(--ink-soft);
  border: 1px dashed var(--rule-strong);
  border-radius: 4px;
  padding: 2.5rem 1.5rem;
  text-align: center;
  font-size: 0.95rem;
}

footer.note {
  margin-top: 3rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--rule);
  font-size: 0.8rem;
  color: var(--ink-soft);
}

@media (max-width: 620px) {
  header.masthead {
    display: block;
    padding-bottom: 1.1rem;
    margin-bottom: 1.35rem;
  }

  .masthead h1 {
    line-height: 1.2;
  }

  .masthead .devanagari-title {
    display: block;
    margin: 0 0 0.15rem;
  }

  .masthead p {
    margin-top: 0.75rem;
    font-size: 0.9rem;
  }

  .wordlist-chip {
    display: inline-block;
    margin-top: 0.85rem;
  }

  .workspace {
    gap: 1.25rem;
  }

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

  .example-row button {
    min-height: 2.75rem;
    padding-inline: 0.55rem;
  }

  .hero {
    gap: 1rem;
    margin-bottom: 1.35rem;
  }

  .hero .score-block { text-align: center; }
  .hero .band-grades { margin-bottom: 0; }

  .scale-labels {
    font-size: 0.65rem;
  }

  .scale-labels span:nth-child(2),
  .scale-labels span:nth-child(4) {
    display: none;
  }

  .contribution-legend {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.35rem;
  }

  .histogram canvas { height: 190px !important; }

  footer.note {
    margin-top: 2rem;
  }
}

@media (max-width: 430px) {
  .shell { padding-inline: 0.75rem; }

  .panel { padding: 0.9rem; }

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

  .slider-row {
    grid-template-columns: minmax(0, 1fr) 3rem;
    gap: 0.45rem;
  }

  .slider-row label { grid-column: 1 / -1; }

  .slider-row input[type="range"] { grid-column: 1; }
  .slider-row .val { grid-column: 2; grid-row: 2; }

  .field-row {
    align-items: flex-start;
    flex-wrap: wrap;
  }

  .field-row label { flex: 1 1 10rem; }

  .field-row input[type="number"] {
    min-height: 2.25rem;
  }

  .placeholder { padding: 1.75rem 1rem; }

  table.word-table th,
  table.word-table td { padding-inline: 0.55rem; }
}
