:root {
  color-scheme: light;
  --paper: #f5f5f5;
  --ink: #111;
  --muted: #555;
  --line: #bdbdbd;
  --panel: #fff;
  --accent: #2f5f9c;
  --accent-dark: #244b7b;
  --warm: #7a4b00;
  --cool: #2f5f9c;
  --shadow: none;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--paper);
  color: var(--ink);
  font-family: "Times New Roman", Times, serif;
}

button,
textarea,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  min-height: 100vh;
  padding: 24px;
  padding-left: max(24px, env(safe-area-inset-left));
  padding-right: max(24px, env(safe-area-inset-right));
  padding-bottom: max(24px, env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 20px;
  max-width: 1480px;
  margin: 0 auto 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--line);
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 400;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(2rem, 4vw, 3rem);
  font-weight: 700;
  letter-spacing: 0;
}

h2 {
  font-size: 1rem;
}

.topbar-actions,
.meter-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.primary-button,
.ghost-button {
  border: 1px solid #777;
  border-radius: 0;
  min-height: 34px;
  padding: 0 12px;
  font-weight: 400;
  background: #fff;
  color: var(--ink);
}

.primary-button {
  background: #e9e9e9;
  color: var(--ink);
}

.primary-button:hover {
  background: #ddd;
}

.ghost-button {
  background: #fff;
}

.workbench {
  display: grid;
  grid-template-columns: minmax(230px, 300px) minmax(340px, 1fr) minmax(260px, 360px);
  grid-template-areas: "tray composer analysis";
  gap: 18px;
  max-width: 1480px;
  margin: 0 auto;
}

.word-panel {
  grid-area: tray;
}

.composer {
  grid-area: composer;
}

.analysis-panel {
  grid-area: analysis;
}

.panel,
.composer {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 0;
  box-shadow: var(--shadow);
}

.panel {
  min-height: 70vh;
  padding: 12px;
}

.panel-header,
.analysis-card-header,
.line-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 0.9rem;
}

.panel-header h2,
.analysis-card-header strong {
  color: var(--ink);
}

.word-form {
  display: grid;
  gap: 8px;
  margin: 18px 0;
}

.word-form label,
.meter-toolbar label {
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 400;
  text-transform: none;
}

textarea,
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 0;
  background: #fff;
  color: var(--ink);
}

textarea {
  resize: vertical;
  min-height: 92px;
  padding: 10px;
  line-height: 1.45;
}

select {
  min-height: 40px;
  padding: 0 10px;
}

.word-tray {
  display: flex;
  align-content: start;
  flex-wrap: wrap;
  gap: 8px;
  max-height: min(58vh, 720px);
  overflow: auto;
  padding-right: 4px;
}

.word-token {
  position: relative;
  isolation: isolate;
  display: inline-flex;
  align-items: end;
  max-width: 100%;
  gap: 1px;
  border: 1px solid #999;
  border-radius: 0;
  background: #f3f3f3;
  color: var(--ink);
  min-height: 44px;
  padding: 12px 18px 5px;
  overflow: visible;
  font-weight: 400;
}

.word-token[data-disambiguated-stress="stressed"] {
  border-color: #8da3c0;
  background: #f0f4f8;
  color: var(--ink);
}

.word-token[data-disambiguated-stress="unstressed"] {
  border-color: #c9b28c;
  background: #f7f3eb;
  color: var(--ink);
}

.word-token[data-availability="missing"] {
  border-color: #a33;
  background: #f8dede;
  color: #5f1111;
}

.word-token[data-availability="exhausted"] {
  border-color: #c9821f;
  background: #f6e2c6;
  color: #6f3d00;
}

.word-token::before,
.word-token::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  z-index: 0;
  width: 16px;
}

.word-token::before {
  left: 0;
}

.word-token::after {
  right: 0;
}

.word-token[data-leading-meter="male"]::before,
.word-token[data-trailing-meter="male"]::after {
  background: #365c8d;
}

.word-token[data-leading-meter="female"]::before,
.word-token[data-trailing-meter="female"]::after {
  background: #b45309;
}

.word-token[data-leading-meter="male"]::before {
  clip-path: polygon(0 0, 100% 50%, 0 100%);
}

.word-token[data-trailing-meter="male"]::after {
  clip-path: polygon(100% 0, 0 50%, 100% 100%);
}

.word-token[data-leading-meter="female"]::before {
  border-radius: 0;
}

.word-token[data-trailing-meter="female"]::after {
  border-radius: 0;
}

.metric-syllable {
  position: relative;
  z-index: 1;
  display: inline-block;
  min-width: 0.55em;
  line-height: 1;
}

.metric-syllable::before {
  position: absolute;
  left: 50%;
  top: -1.05em;
  color: currentColor;
  font-size: 0.85em;
  font-weight: 900;
  transform: translateX(-50%);
}

.metric-syllable[data-stress="1"]::before {
  content: "´";
}

.metric-syllable[data-stress="0"]::before {
  content: "˘";
}

.word-token:hover {
  border-color: var(--accent);
}

.word-tooltip {
  position: fixed;
  z-index: 100;
  display: grid;
  gap: 3px;
  min-width: max-content;
  max-width: min(260px, 82vw);
  padding: 9px 10px;
  border: 1px solid #555;
  border-radius: 0;
  background: #ffffe0;
  box-shadow: none;
  color: #111;
  font-size: 0.8rem;
  font-weight: 400;
  line-height: 1.25;
  opacity: 0;
  pointer-events: none;
  text-align: left;
  transform: translate(-50%, -3px);
  transition:
    opacity 140ms ease,
    transform 140ms ease;
}

.word-tooltip.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.word-token.is-replayed {
  animation: replayPulse 420ms ease-out;
}

.composer {
  min-height: 70vh;
  padding: 12px;
}

.meter-toolbar {
  justify-content: space-between;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}

.meter-toolbar label {
  flex: 1 0 100%;
}

.meter-toolbar select {
  flex: 1 1 220px;
  width: min(260px, 100%);
}

.line-stack {
  display: grid;
  gap: 12px;
  padding-top: 14px;
}

.poem-line {
  min-height: 92px;
  border: 1px solid var(--line);
  border-radius: 0;
  background:
    linear-gradient(transparent 95%, rgba(0, 0, 0, 0.08) 96%),
    #fff;
  background-size: 100% 32px;
  padding: 12px;
}

.line-header {
  margin-bottom: 12px;
}

.line-header-meta {
  display: flex;
  align-items: center;
  gap: 8px;
}

.line-analysis-toggle {
  display: none;
  border: 1px solid var(--line);
  background: #f7f7f7;
  color: var(--muted);
  min-height: 28px;
  padding: 0 8px;
  font-size: 0.8rem;
  line-height: 1;
}

.line-analysis-toggle[aria-expanded="true"] {
  background: #ececec;
  color: var(--ink);
}

.line-words {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0;
  min-height: 42px;
}

.line-input {
  flex: 1 1 140px;
  min-width: 120px;
  height: 44px;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
  font-family: inherit;
  font-size: 1.1rem;
}

.line-input:focus {
  box-shadow: inset 0 -2px 0 rgba(15, 118, 110, 0.35);
}

.empty-line {
  color: var(--muted);
  font-family: inherit;
  font-size: 1.1rem;
}

.line-analysis-inline {
  display: none;
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

.line-analysis-inline p {
  margin: 8px 0 10px;
  font-family: inherit;
  font-size: 1rem;
  overflow-wrap: anywhere;
}

.analysis-list {
  display: grid;
  gap: 10px;
  margin-top: 18px;
}

.analysis-card {
  border: 1px solid var(--line);
  border-radius: 0;
  background: #fff;
  padding: 12px;
}

.analysis-card p {
  margin: 8px 0 10px;
  font-family: inherit;
  font-size: 1.05rem;
  overflow-wrap: anywhere;
}

dl {
  display: grid;
  gap: 6px;
  margin: 0;
}

dl div {
  display: grid;
  grid-template-columns: 82px 1fr;
  gap: 8px;
}

dt {
  color: var(--muted);
  font-size: 0.8rem;
}

dd {
  margin: 0;
  overflow-wrap: anywhere;
  font-weight: 400;
}

@keyframes replayPulse {
  from {
    transform: translateY(-8px);
    background: #fef3c7;
    border-color: var(--warm);
  }
  to {
    transform: translateY(0);
    background: #e9fbf7;
  }
}

@media (max-width: 1060px) {
  .workbench {
    grid-template-columns: 1fr;
    grid-template-areas:
      "composer"
      "tray"
      "analysis";
  }

  .panel,
  .composer {
    min-height: auto;
  }

  .word-tray {
    max-height: none;
    overflow: visible;
    padding-right: 0;
  }
}

@media (max-width: 780px) {
  .app-shell {
    padding: 18px;
    padding-left: max(18px, env(safe-area-inset-left));
    padding-right: max(18px, env(safe-area-inset-right));
    padding-bottom: max(18px, env(safe-area-inset-bottom));
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
    gap: 14px;
  }

  .topbar-actions,
  .meter-toolbar {
    align-items: stretch;
  }

  .topbar-actions > *,
  .meter-toolbar > * {
    width: 100%;
  }

  .panel,
  .composer,
  .poem-line,
  .analysis-card {
    border-radius: 0;
  }

  .line-header,
  .panel-header,
  .analysis-card-header {
    align-items: start;
    flex-direction: column;
    gap: 4px;
  }

  .line-header-meta {
    width: 100%;
    justify-content: space-between;
  }

  .line-analysis-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .line-analysis-inline[hidden] {
    display: none;
  }

  .line-analysis-inline:not([hidden]) {
    display: block;
  }

  .analysis-panel {
    display: none;
  }

  dl div {
    grid-template-columns: 1fr;
    gap: 2px;
  }
}

@media (max-width: 620px) {
  .app-shell {
    padding: 14px;
    padding-left: max(14px, env(safe-area-inset-left));
    padding-right: max(14px, env(safe-area-inset-right));
    padding-bottom: max(14px, env(safe-area-inset-bottom));
  }

  h1 {
    font-size: clamp(2rem, 10vw, 2.8rem);
  }

  .workbench {
    gap: 14px;
  }

  .primary-button,
  .ghost-button {
    width: 100%;
  }

  .panel,
  .composer {
    padding: 14px;
  }

  .word-form {
    margin: 14px 0;
  }

  .word-token {
    min-height: 40px;
    padding: 13px 18px 6px;
    font-size: 0.95rem;
  }

  .poem-line {
    min-height: 80px;
    padding: 10px;
    background-size: 100% 28px;
  }

  .line-words {
    gap: 0;
  }

  .line-input,
  .empty-line,
  .analysis-card p {
    font-size: 1rem;
  }

  .word-tooltip {
    max-width: min(240px, calc(100vw - 24px));
  }
}
