:root {
  color-scheme: light;
  /* Anthropic-inspired palette: cream paper, sand structure, charcoal ink */
  --bg: #e8e6dc;
  --surface: #faf9f5;
  --surface-2: #ecebe3;
  --text: #141413;
  --muted: #55534b; /* darkened for low-vision contrast (>= 6:1 on cream) */
  --line: #b9b7ad;
  --accent: #d97757; /* Anthropic orange */
  --accent-strong: #c4613f;
  --focus: #1f5fa8;
  --danger: #8e2424;
  --ok: #3d5a24;
  --shadow: 0 8px 24px rgba(20, 20, 19, 0.08);
  --radius: 10px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 17px;
  line-height: 1.45;
  letter-spacing: 0.01em;
}

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

button {
  min-height: 44px;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  padding: 0 16px;
  cursor: pointer;
  font-weight: 600;
}

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

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--text);
  font-weight: 700;
}

button.primary:hover {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}

button.big {
  min-height: 50px;
  padding: 0 22px;
  font-size: 18px;
}

button.quiet {
  background: transparent;
  border-color: transparent;
  text-decoration: underline;
  text-underline-offset: 3px;
}

button.quiet:hover {
  border-color: var(--line);
}

button.danger {
  color: var(--danger);
  border-color: rgba(142, 36, 36, 0.45);
}

button.danger.solid {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

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

input,
select,
textarea {
  width: 100%;
  min-height: 46px;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 10px 12px;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--focus);
}

textarea {
  resize: vertical;
}

fieldset {
  border: 0;
  margin: 0;
  padding: 0;
  min-width: 0;
}

legend,
.field-label {
  display: block;
  margin-bottom: 8px;
  color: var(--text);
  font-size: 16px;
  font-weight: 700;
  padding: 0;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.hidden {
  display: none !important;
}

.muted {
  color: var(--muted);
}

.hint {
  color: var(--muted);
  font-size: 15px;
  margin: 6px 0 0;
}

/* Mode switching: viewer hides edit controls and vice versa */
body.mode-viewer .edit-only,
body.mode-loading .edit-only,
body.mode-edit .view-only,
body.mode-loading .view-only {
  display: none !important;
}

.app-shell {
  min-height: 100vh;
  padding: 18px;
  max-width: 1500px;
  margin: 0 auto;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.topbar h1 {
  margin: 0;
  font-size: 30px;
  line-height: 1.1;
}

.mode-line {
  margin: 4px 0 0;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  align-items: center;
}

.save-state {
  font-weight: 700;
  margin: 0;
}

.save-state[data-kind="saved"] {
  color: var(--ok);
}

.save-state[data-kind="pending"],
.save-state[data-kind="saving"] {
  color: var(--muted);
}

.save-state[data-kind="error"] {
  color: var(--danger);
}

.top-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.banner {
  display: flex;
  gap: 14px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 14px;
  padding: 12px 16px;
  border-radius: var(--radius);
  background: #fbe9e2;
  border: 1.5px solid var(--accent);
  font-weight: 650;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

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

.stat {
  padding: 12px 16px;
}

.stat span {
  display: block;
  font-size: 28px;
  font-weight: 760;
}

.stat small {
  color: var(--muted);
  font-size: 15px;
}

.panel {
  padding: 16px;
}

.panel h2,
.board-section h2 {
  margin: 0;
  font-size: 20px;
}

.content {
  display: grid;
  gap: 16px;
  min-width: 0;
}

.toolbar {
  display: grid;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--shadow);
}

.search-box {
  display: block;
}

.tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.tab {
  background: var(--surface-2);
}

.tab.active {
  background: var(--text);
  color: var(--surface);
  border-color: var(--text);
}

.display-controls {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.control-group {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  flex-wrap: wrap;
  padding: 4px;
  border-radius: 10px;
  background: var(--surface-2);
}

.control-label {
  padding: 0 6px;
  color: var(--muted);
  font-size: 15px;
  font-weight: 700;
}

.display-toggle {
  min-height: 40px;
  padding: 0 12px;
  background: var(--surface);
  border-color: transparent;
  font-size: 15px;
}

.display-toggle.active {
  background: var(--text);
  color: var(--surface);
  border-color: var(--text);
}

.board-section {
  min-width: 0;
}

.section-heading {
  display: flex;
  gap: 8px;
  align-items: baseline;
  justify-content: space-between;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(175px, 1fr));
  gap: 14px;
}

.movie-card {
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
  box-shadow: var(--shadow);
  text-align: left;
  display: flex;
  flex-direction: column;
}

.movie-card.editable {
  cursor: pointer;
}

.movie-card.editable[draggable="true"] {
  cursor: grab;
}

.movie-card.editable:hover {
  border-color: var(--text);
}

.movie-card.dragging {
  opacity: 0.45;
}

.movie-card.drop-before {
  box-shadow: -5px 0 0 0 var(--accent), var(--shadow);
}

.movie-card.drop-after {
  box-shadow: 5px 0 0 0 var(--accent), var(--shadow);
}

.movie-card.flash {
  outline: 4px solid var(--accent);
  outline-offset: 2px;
  animation: flash 0.9s ease-in-out 2;
}

@keyframes flash {
  50% { outline-color: transparent; }
}

.poster {
  position: relative;
  width: 100%;
  aspect-ratio: 2 / 3;
  background: var(--surface-2);
  display: grid;
  place-items: center;
  color: var(--muted);
  font-weight: 760;
  overflow: hidden;
}

.poster img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.poster-fallback {
  padding: 12px;
  text-align: center;
  word-break: break-word;
}

.critic-score {
  position: absolute;
  right: 8px;
  top: 8px;
  min-width: 48px;
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-radius: 999px;
  padding: 5px 8px;
  background: rgba(20, 20, 19, 0.85);
  color: #ffffff;
  font-size: 15px;
  font-weight: 760;
  line-height: 1;
  text-align: center;
}

.card-body {
  display: grid;
  gap: 8px;
  padding: 12px;
}

.card-title {
  font-size: 17px;
  font-weight: 760;
  line-height: 1.25;
}

.card-meta,
.pill-row {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
}

.card-meta {
  color: var(--muted);
  font-size: 15px;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  border-radius: 999px;
  padding: 2px 10px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

.pill.status-tbr {
  background: #dfe9f4;
  color: #23465f;
}

.pill.status-watching {
  background: #f6ded2;
  color: #7a3317;
}

.pill.status-watched {
  background: #e1e7d1;
  color: #34401f;
}

.pill.status-dnf {
  background: #e9e9e6;
  color: #4d4d48;
}

.pill.rating {
  background: var(--text);
  color: var(--surface);
}

.watch-log,
.ranking-list {
  display: grid;
  gap: 4px;
}

#logView .panel {
  max-width: 920px;
  margin: 0 auto;
}

.watch-row,
.ranking-row {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 56px;
  text-align: left;
  border: 0;
  border-top: 1px solid var(--line);
  border-radius: 0;
  background: transparent;
  padding: 8px 6px;
  font-weight: 400;
}

.watch-row.editable:hover,
.ranking-row.editable:hover {
  background: var(--surface-2);
}

.watch-row:first-child,
.ranking-row:first-child {
  border-top: 0;
}

.row-title {
  font-weight: 720;
}

.row-note {
  color: var(--muted);
  font-size: 15px;
  margin-top: 2px;
}

.empty-state {
  color: var(--muted);
  background: var(--surface-2);
  border-radius: 8px;
  padding: 14px;
}

/* ---- Dialogs ---- */

.dialog {
  width: min(760px, calc(100vw - 24px));
  max-height: calc(100vh - 32px);
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  padding: 22px;
  box-shadow: 0 24px 60px rgba(20, 20, 19, 0.3);
  overflow: auto;
}

.dialog.small {
  width: min(440px, calc(100vw - 24px));
}

.dialog::backdrop {
  background: rgba(20, 20, 19, 0.55);
}

.dialog h2 {
  margin: 0;
  font-size: 24px;
  line-height: 1.2;
}

.dialog > * + * {
  margin-top: 18px;
}

.dialog-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 14px;
}

.dialog-foot {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 14px;
  flex-wrap: wrap;
  border-top: 1px solid var(--line);
  padding-top: 18px;
}

.form-stack {
  display: grid;
  gap: 12px;
}

.form-stack .field-label,
.form-stack label {
  font-weight: 700;
}

.form-error {
  margin: 0;
  color: var(--danger);
  font-weight: 700;
}

.button-row {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.button-row.left {
  justify-content: flex-start;
}

.big-input {
  min-height: 56px;
  font-size: 20px;
}

/* Segmented control built from radio buttons (keyboard arrows work natively) */
.segments {
  display: grid;
  gap: 6px;
  padding: 5px;
  border-radius: 12px;
  background: var(--surface-2);
}

.segments.two {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.segments.four {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.segments label {
  position: relative;
  display: block;
}

.segments input {
  position: absolute;
  opacity: 0;
  inset: 0;
  margin: 0;
  min-height: 0;
  cursor: pointer;
}

.segments span {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 50px;
  padding: 6px 8px;
  border-radius: 9px;
  border: 1.5px solid transparent;
  text-align: center;
  font-weight: 700;
  cursor: pointer;
}

.segments label:hover span {
  border-color: var(--text);
}

.segments input:checked + span {
  background: var(--text);
  color: var(--surface);
}

.segments input:checked + span::before {
  content: "✓ ";
  white-space: pre;
}

.segments input:focus-visible + span {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

/* Add dialog results */
.search-results {
  display: grid;
  gap: 8px;
  max-height: min(52vh, 520px);
  overflow: auto;
  padding: 2px;
}

.search-result {
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr) auto;
  gap: 14px;
  align-items: center;
  min-height: 92px;
  padding: 8px 12px 8px 8px;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  background: #fff;
  cursor: pointer;
}

.search-result:hover,
.search-result.active {
  border-color: var(--text);
  background: #fdf3ee;
}

.search-result.active {
  outline: 3px solid var(--focus);
  outline-offset: 1px;
}

.search-result img,
.thumb-fallback {
  width: 56px;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  border-radius: 6px;
  background: var(--surface-2);
  display: block;
}

.result-text {
  display: grid;
  gap: 2px;
  font-size: 18px;
}

.result-action {
  font-weight: 760;
  padding: 8px 14px;
  border-radius: 8px;
  background: var(--accent);
}

.result-action.on-list {
  background: var(--surface-2);
  color: var(--muted);
}

.manual-add {
  justify-self: start;
}

/* Edit dialog */
.edit-title-row {
  display: flex;
  gap: 14px;
  align-items: center;
}

.edit-poster {
  flex: 0 0 60px;
  width: 60px;
  aspect-ratio: 2 / 3;
  border-radius: 6px;
  overflow: hidden;
  background: var(--surface-2);
  display: grid;
  place-items: center;
  font-size: 24px;
  font-weight: 760;
  color: var(--muted);
}

.edit-poster img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#editMeta {
  margin: 4px 0 0;
}

.rating-buttons {
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 6px;
}

.rating-buttons button {
  min-width: 0;
  min-height: 52px;
  padding: 0;
  font-size: 19px;
  font-weight: 760;
}

.rating-buttons button.active {
  background: var(--text);
  border-color: var(--text);
  color: var(--surface);
}

.rating-foot {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  margin-top: 6px;
}

.rating-foot .hint {
  margin: 0;
}

.chips {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 10px;
  padding: 0;
}

.chip {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 16px;
}

.chip:hover {
  border-color: var(--danger);
}

.chip-x {
  font-size: 14px;
  color: var(--danger);
}

.no-tags {
  padding: 6px 0;
}

.inline-add {
  display: flex;
  gap: 8px;
}

.inline-add button {
  flex: 0 0 auto;
}

.advanced summary {
  cursor: pointer;
  font-weight: 700;
  min-height: 44px;
  display: flex;
  align-items: center;
}

.advanced-grid {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr;
  gap: 12px;
  margin: 10px 0 12px;
}

.remove-confirm {
  padding: 12px 14px;
  border-radius: 10px;
  background: #f8e3e3;
  border: 1.5px solid var(--danger);
}

.remove-confirm p {
  margin: 0 0 10px;
  font-weight: 700;
}

/* Toast with optional action (Undo / Try again) */
.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 10;
  display: flex;
  gap: 14px;
  align-items: center;
  min-width: 240px;
  max-width: min(520px, calc(100vw - 36px));
  padding: 14px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--surface);
  font-size: 17px;
  box-shadow: var(--shadow);
  transform: translateY(100px);
  opacity: 0;
  transition: opacity 160ms ease, transform 160ms ease;
  pointer-events: none;
}

.toast.visible {
  transform: translateY(0);
  opacity: 1;
  pointer-events: auto;
}

.toast button {
  flex: 0 0 auto;
  background: var(--accent);
  border-color: var(--accent);
  color: var(--text);
  font-weight: 760;
}

@media (prefers-reduced-motion: reduce) {
  .toast,
  .movie-card.flash {
    transition: none;
    animation: none;
  }
}

@media (max-width: 760px) {
  .app-shell {
    padding: 12px;
  }

  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .card-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .segments.four {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .rating-buttons {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }

  .advanced-grid {
    grid-template-columns: 1fr;
  }

  .search-result {
    grid-template-columns: 48px minmax(0, 1fr);
  }

  .result-action {
    grid-column: 2;
    justify-self: start;
  }

  .dialog {
    padding: 16px;
  }
}
