:root {
  color-scheme: light;

  /* -- paper & ink -- */
  --paper: #eef1e8;
  --paper-deep: #e4e8dc;
  --card: #f8faf4;
  --ink: #1b2a24;
  --ink-soft: #56655c;
  --ink-faint: #8b978c;
  --line: #cbd4c4;
  --line-strong: #aab5a1;

  /* -- measurement inks: blue for quantity, brass for unit -- */
  --quantity: #1f6f86;
  --quantity-deep: #0e4a5a;
  --quantity-bg: #dcecf1;
  --measure: #a8721a;
  --measure-deep: #7a5210;
  --measure-bg: #f3e6c6;

  --ok: #2f7d4f;
  --error: #b23b2e;
  --error-bg: #f7e2df;

  --font-display: "Fraunces", "Iowan Old Style", Georgia, serif;
  --font-body: "IBM Plex Sans", "Segoe UI", Arial, sans-serif;
  --font-mono: "IBM Plex Mono", Consolas, "Courier New", monospace;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--paper);
  background-image:
    radial-gradient(ellipse at top left, rgba(31, 111, 134, 0.06), transparent 55%),
    radial-gradient(ellipse at bottom right, rgba(168, 114, 26, 0.06), transparent 55%);
  color: var(--ink);
  font-family: var(--font-body);
}

/* ---------- header ---------- */

.topbar {
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
  padding: 26px 36px 20px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--quantity);
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.eyebrow::before {
  content: "";
  display: inline-block;
  width: 16px;
  height: 1px;
  margin-right: 8px;
  background: var(--measure);
  vertical-align: middle;
}

h1, h2, p {
  margin: 0;
}

h1 {
  font-family: var(--font-display);
  font-size: 30px;
  font-weight: 600;
  font-optical-sizing: auto;
  line-height: 1.15;
  letter-spacing: -0.01em;
  color: var(--ink);
}

h2 {
  font-family: var(--font-display);
  font-size: 19px;
  font-weight: 600;
  line-height: 1.3;
}

.ruler {
  height: 10px;
  background-image: repeating-linear-gradient(
    90deg,
    var(--line-strong) 0,
    var(--line-strong) 1px,
    transparent 1px,
    transparent 12px
  );
  background-position: 4px 0;
  border-bottom: 1px solid var(--line);
  opacity: 0.7;
}

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

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 1fr);
  gap: 22px;
  width: min(1080px, calc(100vw - 40px));
  margin: 28px auto 44px;
}

.workspace,
.tokens {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--card);
  box-shadow: 0 1px 0 rgba(27, 42, 36, 0.03);
}

.workspace {
  padding: 22px 22px 20px;
}

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px dashed var(--line-strong);
}

.section-head .step {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-faint);
  letter-spacing: 0.08em;
}

button {
  min-height: 40px;
  border: 1px solid var(--quantity-deep);
  border-radius: 3px;
  padding: 0 18px;
  color: var(--card);
  background: var(--quantity);
  font: inherit;
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.03em;
  cursor: pointer;
  transition: background 0.12s ease, transform 0.05s ease;
}

button:hover {
  background: var(--quantity-deep);
}

button:active {
  transform: translateY(1px);
}

button:disabled {
  cursor: wait;
  opacity: 0.6;
}

textarea {
  width: 100%;
  min-height: 140px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 3px;
  padding: 14px;
  color: var(--ink);
  background: var(--paper);
  font: 17px/1.6 var(--font-mono);
  outline: none;
}

textarea:focus {
  border-color: var(--quantity);
  box-shadow: 0 0 0 3px rgba(31, 111, 134, 0.12);
}

.actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 14px;
}

.hint {
  color: var(--ink-faint);
  font-size: 12px;
  font-family: var(--font-mono);
}

/* ---------- loading overlay ---------- */

.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(27, 42, 36, 0.28);
  backdrop-filter: blur(2px);
}

.loading-overlay[hidden] {
  display: none;
}

.loading-box {
  display: grid;
  justify-items: center;
  gap: 10px;
  width: min(360px, 100%);
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  padding: 24px 26px;
  color: var(--ink);
  background: var(--card);
  box-shadow: 0 18px 40px rgba(27, 42, 36, 0.18);
  text-align: center;
}

.loading-box strong {
  font-family: var(--font-display);
  font-size: 20px;
  font-weight: 600;
}

.loading-box span {
  color: var(--ink-soft);
  font-size: 13px;
  line-height: 1.45;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--line);
  border-top-color: var(--quantity);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---------- result: highlighted sentence ---------- */

.highlighted {
  min-height: 96px;
  border: 1px solid var(--line);
  border-radius: 3px;
  padding: 18px 20px;
  font-size: 21px;
  line-height: 2;
  background: var(--paper);
  color: var(--ink);
}

.entity {
  display: inline;
  border-bottom: 2.5px solid;
  padding: 1px 3px;
  border-radius: 2px;
}

.entity.quantity {
  border-color: var(--quantity);
  background: var(--quantity-bg);
}

.entity.measure {
  border-color: var(--measure);
  background: var(--measure-bg);
}

.entities {
  display: grid;
  gap: 8px;
  margin-top: 16px;
}

.entity-row {
  display: grid;
  grid-template-columns: 128px 1fr 78px;
  gap: 12px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 3px;
  padding: 10px 12px;
  background: var(--paper);
}

.tag {
  width: max-content;
  border-radius: 2px;
  padding: 3px 8px;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.03em;
}

.tag.quantity {
  color: var(--quantity-deep);
  background: var(--quantity-bg);
  border: 1px solid var(--quantity);
}

.tag.measure {
  color: var(--measure-deep);
  background: var(--measure-bg);
  border: 1px solid var(--measure);
}

.muted {
  color: var(--ink-soft);
  font-size: 13px;
}

.span {
  color: var(--ink-faint);
  font-family: var(--font-mono);
  font-size: 12px;
  text-align: right;
}

/* ---------- token ledger: interlinear gloss ---------- */

.tokens {
  padding: 22px;
}

.gloss-line {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px 3px;
  margin-top: 4px;
  padding: 6px 4px 18px;
}

.gloss-group {
  display: flex;
  align-items: flex-end;
  position: relative;
  padding-bottom: 38px;
}

.gloss-group.tagged {
  margin: 0 2px;
}

.gloss-token {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 6px 8px 4px;
  min-width: 44px;
}

.gloss-token .word {
  font-family: var(--font-body);
  font-size: 15px;
  font-weight: 600;
  color: var(--ink);
  white-space: nowrap;
}

.gloss-token .bio {
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--ink-faint);
  white-space: nowrap;
}

.gloss-group.tagged .gloss-token .word {
  color: var(--quantity-deep);
}

.gloss-group.tagged.measure .gloss-token .word {
  color: var(--measure-deep);
}

.gloss-bracket {
  position: absolute;
  left: 4px;
  right: 4px;
  bottom: 18px;
  height: 16px;
  border-bottom: 2px solid var(--quantity);
  border-left: 2px solid var(--quantity);
  border-right: 2px solid var(--quantity);
  border-radius: 0 0 3px 3px;
}

.gloss-group.measure .gloss-bracket {
  border-color: var(--measure);
}

.gloss-label {
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--quantity-deep);
  background: var(--card);
  padding: 0 4px;
  white-space: nowrap;
}

.gloss-group.measure .gloss-label {
  color: var(--measure-deep);
}

.gloss-group:not(.tagged) .gloss-token .word {
  color: var(--ink-soft);
}

.gloss-group:not(.tagged) .gloss-token .bio {
  color: #b7c0ac;
}

/* ---------- responsive ---------- */

@media (max-width: 820px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
    gap: 14px;
    padding: 20px;
  }

  .layout {
    width: min(100vw - 28px, 720px);
    margin-top: 14px;
  }

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

  .span {
    text-align: left;
  }

  h1 {
    font-size: 24px;
  }
}

@media (prefers-reduced-motion: reduce) {
  button {
    transition: none;
  }

  .spinner {
    animation: none;
  }
}
