:root {
  --canvas: #f7f5ef;
  --surface: #ffffff;
  --surface-soft: #f1eee7;
  --ink: #1f2328;
  --muted: #667085;
  --subtle: #98a2b3;
  --line: #ded8cc;
  --primary: #2563eb;
  --primary-pressed: #1d4ed8;
  --success: #16803c;
  --warning: #b45309;
  --danger: #c2410c;
  --focus: #0f766e;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Inter, "Segoe UI", "Microsoft YaHei", sans-serif;
  color: var(--ink);
  background: var(--canvas);
  letter-spacing: 0;
}

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

button {
  cursor: pointer;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 240px 1fr;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 18px;
  border-right: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.74);
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 28px;
}

.brand-mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: #fff;
  background: var(--ink);
  font-weight: 700;
}

.brand span,
.metric-card span,
.quick-card span,
.code-meta,
.record-meta,
.eyebrow {
  color: var(--muted);
}

.brand strong,
.brand span {
  display: block;
}

.nav {
  display: grid;
  gap: 8px;
}

.nav-item,
.segment,
.button,
.quick-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
}

.nav-item {
  height: 40px;
  padding: 0 12px;
  text-align: left;
}

.nav-item.active,
.segment.active {
  border-color: rgba(37, 99, 235, 0.32);
  background: rgba(37, 99, 235, 0.1);
  color: var(--primary-pressed);
  font-weight: 600;
}

.main {
  width: min(1180px, 100%);
  padding: 34px;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.page-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 20px;
  margin-bottom: 24px;
}

.page-head.compact {
  align-items: center;
}

.eyebrow {
  margin: 0 0 8px;
  font-size: 13px;
  font-weight: 700;
}

h1,
h2 {
  margin: 0;
  line-height: 1.15;
}

h1 {
  font-size: 34px;
}

h2 {
  font-size: 22px;
}

.toolbar,
.settings-row,
.segmented,
.modal-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.toolbar > .input {
  width: auto;
  min-width: 138px;
}

.button,
.segment {
  min-height: 38px;
  padding: 9px 13px;
}

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

.button.primary:hover {
  background: var(--primary-pressed);
}

.button.danger {
  color: var(--danger);
}

.input {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  background: var(--surface);
  color: var(--ink);
}

.range-input {
  padding-inline: 0;
}

.input:focus,
.button:focus,
.nav-item:focus,
.segment:focus,
.quick-card:focus,
.icon-button:focus {
  outline: 2px solid rgba(15, 118, 110, 0.28);
  outline-offset: 2px;
}

.metric-grid,
.quick-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.metric-card,
.quick-card,
.code-card,
.stage,
.record-item,
.modal-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.metric-card {
  padding: 18px;
}

.metric-card strong {
  display: block;
  margin-top: 8px;
  font-size: 28px;
}

.quick-card {
  min-height: 118px;
  padding: 18px;
  text-align: left;
}

.quick-card strong,
.quick-card span {
  display: block;
}

.quick-card span {
  margin-top: 10px;
  line-height: 1.5;
}

.mobile-guide {
  display: none;
}

.code-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 12px;
}

.code-card {
  overflow: hidden;
}

.code-card img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: contain;
  display: block;
  background: var(--surface-soft);
}

.code-info {
  padding: 12px;
}

.code-title {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
  margin-bottom: 6px;
  font-weight: 700;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 22px;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 12px;
}

.badge-row {
  display: inline-flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 4px;
}

.badge.custom {
  color: var(--primary-pressed);
  background: rgba(37, 99, 235, 0.1);
}

.stage {
  margin-top: 16px;
  min-height: 320px;
  padding: 20px;
}

.stage.empty {
  display: grid;
  place-items: center;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.58);
}

.training-panel {
  display: none;
}

.training-panel.active {
  display: block;
}

.settings-row label {
  display: grid;
  gap: 6px;
  min-width: 160px;
  color: var(--muted);
  font-size: 14px;
}

.custom-digit-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: 10px;
  margin-top: 12px;
}

.custom-digit-row label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 14px;
}

.custom-digit-input {
  min-height: 72px;
  resize: vertical;
}

.digit-display {
  font-family: "Cascadia Mono", Consolas, monospace;
  line-height: 1.8;
  word-break: break-all;
  font-size: 24px;
  letter-spacing: 0;
}

.timer-line {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

.answer-box {
  width: 100%;
  min-height: 150px;
  resize: vertical;
}

.digit-pad {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  margin: 12px 0;
}

.pad-key {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  color: var(--ink);
  font-weight: 700;
}

.pad-key.wide {
  grid-column: span 2;
  color: var(--muted);
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin: 16px 0;
}

.result-cell {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--surface-soft);
}

.mistake-list {
  max-height: 180px;
  overflow: auto;
  margin-top: 12px;
  color: var(--danger);
  font-family: "Cascadia Mono", Consolas, monospace;
}

.compare-panel {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-top: 16px;
}

.compare-panel > div {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--surface-soft);
}

.digit-compare {
  margin: 8px 0 0;
  font-family: "Cascadia Mono", Consolas, monospace;
  line-height: 1.7;
  word-break: break-all;
  letter-spacing: 0;
}

.link-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(138px, 1fr));
  gap: 12px;
}

.link-card {
  position: relative;
  display: grid;
  gap: 6px;
  align-content: start;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: var(--surface);
}

.link-card img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: contain;
  border-radius: 6px;
  background: var(--surface-soft);
}

.link-card .poker-face {
  aspect-ratio: 5 / 7;
}

.poker-link-card {
  grid-template-columns: 0.72fr 1fr;
}

.poker-link-card .link-index,
.poker-link-card strong,
.poker-link-card span {
  grid-column: 1 / -1;
}

.link-index {
  position: absolute;
  top: 8px;
  left: 8px;
  min-width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.9);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.link-card strong {
  font-size: 18px;
}

.link-card span:not(.link-index) {
  color: var(--muted);
  font-size: 13px;
}

.quiz-card {
  max-width: 520px;
}

.quiz-code {
  font-size: 64px;
  font-weight: 800;
  margin: 16px 0;
}

.quiz-image-wrap {
  width: min(var(--quiz-image-size, 132px), 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  overflow: hidden;
  margin: 14px 0;
}

.quiz-main-image {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: contain;
  display: block;
}

.quiz-main-image.poker-face {
  aspect-ratio: 5 / 7;
}

.quiz-options {
  display: grid;
  gap: 10px;
  margin: 14px 0;
}

.image-options {
  grid-template-columns: repeat(auto-fit, minmax(var(--quiz-choice-size, 148px), var(--quiz-choice-size, 148px)));
  max-width: min(100%, calc(var(--quiz-choice-size, 148px) * 3 + 10px * 2));
  justify-content: start;
}

.code-options {
  grid-template-columns: repeat(3, minmax(0, 92px));
}

.quiz-option {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
}

.quiz-option:hover,
.pad-key:hover {
  border-color: rgba(37, 99, 235, 0.42);
  background: rgba(37, 99, 235, 0.08);
}

.quiz-option.correct {
  border-color: rgba(22, 128, 60, 0.75);
  background: rgba(22, 128, 60, 0.1);
}

.quiz-option.wrong {
  border-color: rgba(194, 65, 12, 0.75);
  background: rgba(194, 65, 12, 0.1);
}

.quiz-option:disabled {
  cursor: default;
}

.quiz-feedback {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
}

.quiz-feedback.correct strong {
  color: var(--success);
}

.quiz-feedback.wrong strong {
  color: var(--danger);
}

.image-choice {
  width: var(--quiz-choice-size, 148px);
  padding: 8px;
}

.image-choice img {
  width: min(var(--quiz-image-size, 132px), 100%);
  margin-inline: auto;
  aspect-ratio: 4 / 3;
  object-fit: contain;
  display: block;
  border-radius: 6px;
  background: var(--surface-soft);
}

.image-choice img.poker-face {
  aspect-ratio: 5 / 7;
}

.image-choice span {
  display: block;
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
}

.code-choice {
  min-height: 56px;
  font-size: 24px;
  font-weight: 800;
}

.poker-memory-grid,
.poker-picker {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(82px, 1fr));
  gap: 10px;
}

.poker-card {
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
}

.poker-card img,
.poker-pick img {
  width: 100%;
  aspect-ratio: 5 / 7;
  object-fit: contain;
  display: block;
  background: var(--surface-soft);
}

.poker-card figcaption,
.poker-pick span,
.poker-mini span {
  display: block;
  padding: 6px;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}

.poker-answer-strip {
  min-height: 82px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  padding: 10px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
}

.poker-mini {
  width: 54px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
}

.poker-mini img {
  width: 100%;
  aspect-ratio: 5 / 7;
  object-fit: contain;
  display: block;
}

.poker-pick {
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
}

.poker-pick:hover:not(:disabled) {
  border-color: rgba(37, 99, 235, 0.42);
  background: rgba(37, 99, 235, 0.08);
}

.poker-pick.selected {
  opacity: 0.38;
  cursor: not-allowed;
}

.record-list {
  display: grid;
  gap: 10px;
}

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

.wrong-review h3 {
  margin: 0;
  font-size: 18px;
}

.wrong-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
}

.record-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 14px;
  padding: 14px 16px;
}

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

.record-detail {
  grid-column: 1 / -1;
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.record-detail.hidden {
  display: none;
}

.compare-panel.compact {
  margin-top: 0;
}

.record-item strong,
.record-meta {
  display: block;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 22px;
  background: rgba(31, 35, 40, 0.36);
}

.modal.hidden {
  display: none;
}

.modal-panel {
  width: min(760px, 100%);
  max-height: 92vh;
  overflow: auto;
  padding: 20px;
}

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 18px;
  margin-bottom: 18px;
}

.icon-button {
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font-size: 24px;
  line-height: 1;
}

.editor-body {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 18px;
}

.preview-box {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  overflow: hidden;
}

.preview-box img {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: contain;
  display: block;
}

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

.form-stack label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 14px;
}

.file-button {
  position: relative;
  overflow: hidden;
  display: inline-flex;
  justify-content: center;
  align-items: center;
}

.file-button input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

@media (max-width: 820px) {
  html {
    -webkit-text-size-adjust: 100%;
  }

  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    inset: auto 0 0 0;
    z-index: 4;
    height: auto;
    padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
    border-right: 0;
    border-top: 1px solid var(--line);
  }

  .brand {
    display: none;
  }

  .nav {
    grid-template-columns: repeat(4, 1fr);
  }

  .nav-item {
    min-height: 44px;
    padding: 0 4px;
    text-align: center;
  }

  .main {
    min-width: 0;
    padding: 22px 14px calc(92px + env(safe-area-inset-bottom));
  }

  .mobile-guide {
    display: block;
    margin: 8px 0 0;
    padding: 12px 14px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    color: var(--muted);
    font-size: 14px;
    line-height: 1.55;
  }

  .button,
  .segment,
  .input,
  .pad-key,
  .icon-button {
    min-height: 44px;
  }

  .input,
  textarea {
    font-size: 16px;
  }

  .segmented {
    flex-wrap: nowrap;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    padding: 2px 2px 8px;
    scrollbar-width: thin;
  }

  .segment {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  .page-head,
  .page-head.compact {
    display: grid;
  }

  .metric-grid,
  .quick-grid,
  .result-grid,
  .compare-panel,
  .custom-digit-row,
  .editor-body {
    grid-template-columns: 1fr;
  }

  .metric-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
  }

  .metric-card {
    min-width: 0;
    padding: 12px 10px;
  }

  .metric-card span {
    font-size: 12px;
    line-height: 1.4;
  }

  .metric-card strong {
    font-size: 22px;
    overflow-wrap: anywhere;
  }

  .quick-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .quick-card {
    min-height: 132px;
    padding: 14px;
  }

  .quick-card span {
    font-size: 13px;
    line-height: 1.45;
  }

  .image-options {
    grid-template-columns: repeat(auto-fit, minmax(var(--quiz-choice-size, 148px), var(--quiz-choice-size, 148px)));
  }

  .toolbar > .input {
    width: 100%;
  }

  .code-title {
    flex-wrap: wrap;
  }

  .badge-row {
    justify-content: flex-start;
  }

  .modal {
    padding: 12px 12px calc(12px + env(safe-area-inset-bottom));
  }

  .modal-panel {
    max-height: calc(100dvh - 24px - env(safe-area-inset-bottom));
    padding: 16px;
  }

  .record-item {
    grid-template-columns: 1fr;
  }

  .record-actions {
    justify-content: flex-start;
  }

  h1 {
    font-size: 28px;
  }
}
