:root {
  --bg: #f4f5f1;
  --surface: #ffffff;
  --surface-muted: #eef1eb;
  --ink: #18201d;
  --ink-muted: #5c6862;
  --line: #d9ded6;
  --primary: #155e63;
  --primary-pressed: #0e494d;
  --accent: #a85f2f;
  --danger: #a63a32;
  --shadow: 0 10px 28px rgba(20, 31, 27, 0.12);
  --radius: 8px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
}

body {
  color: var(--ink);
  background: var(--bg);
  font-family: "Microsoft YaHei", "PingFang SC", "Noto Sans CJK SC", sans-serif;
  line-height: 1.55;
}

button {
  font: inherit;
  touch-action: manipulation;
}

.app-shell {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  width: 100%;
  max-width: 720px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  min-width: 0;
  padding: calc(var(--space-3) + env(safe-area-inset-top)) var(--space-4) var(--space-3);
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.title-block {
  min-width: 0;
}

.app-title {
  overflow: hidden;
  color: var(--ink);
  font-size: 20px;
  font-weight: 700;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meta-line {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
  margin-top: var(--space-1);
  color: var(--ink-muted);
  font-size: 13px;
  white-space: nowrap;
}

.dot {
  width: 4px;
  height: 4px;
  border-radius: 999px;
  background: var(--accent);
}

.top-button,
.action-button {
  min-width: 44px;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--ink);
  background: var(--surface);
  cursor: pointer;
  transition: background-color 140ms ease, border-color 140ms ease, color 140ms ease, transform 120ms ease;
}

.top-button {
  flex: 0 0 auto;
  padding: 0 var(--space-4);
}

.action-button {
  width: 100%;
  padding: 0 var(--space-2);
  font-size: 15px;
  font-weight: 600;
}

.top-button:hover:not(:disabled),
.action-button:hover:not(:disabled),
.study-card:hover {
  border-color: color-mix(in oklch, var(--primary), var(--line) 45%);
  background: #fbfcf9;
}

.top-button:active:not(:disabled),
.action-button:active:not(:disabled),
.study-card:active {
  transform: translateY(1px);
}

.top-button:focus-visible,
.action-button:focus-visible,
.study-card:focus-visible,
.panel-close:focus-visible,
.search-input:focus-visible,
.search-result:focus-visible,
.image-button:focus-visible,
.preview-close:focus-visible {
  outline: 3px solid color-mix(in oklch, var(--primary), white 68%);
  outline-offset: 2px;
}

.top-button:disabled,
.action-button:disabled {
  color: #8b948e;
  background: #f0f2ee;
  cursor: not-allowed;
}

.action-button.primary {
  border-color: var(--primary);
  color: #ffffff;
  background: var(--primary);
}

.action-button.primary:hover:not(:disabled) {
  border-color: var(--primary-pressed);
  background: var(--primary-pressed);
}

.action-button.active {
  border-color: var(--accent);
  color: #ffffff;
  background: var(--accent);
}

.study-area {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: var(--space-4);
  background: var(--bg);
}

.study-card {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: var(--space-5);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 140ms ease, border-color 140ms ease, transform 120ms ease;
}

.card-title {
  display: block;
  margin: 0 0 var(--space-4);
  color: var(--ink);
  font-size: 22px;
  font-weight: 700;
  line-height: 1.38;
  letter-spacing: 0;
  text-wrap: pretty;
}

.card-title-text {
  display: inline;
  white-space: pre-line;
}

.item-id {
  display: inline-flex;
  align-items: center;
  max-width: 5.5em;
  min-height: 1.65em;
  margin-right: var(--space-2);
  padding: 0 0.6em;
  overflow: hidden;
  border-radius: 999px;
  color: var(--ink-muted);
  background: var(--surface-muted);
  font-size: 0.7em;
  font-weight: 700;
  line-height: 1;
  text-overflow: ellipsis;
  vertical-align: 0.08em;
  white-space: nowrap;
}

.item-id[hidden] {
  display: none;
}

.page-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-1) var(--space-2);
  max-width: 100%;
  min-width: 0;
  margin: calc(-1 * var(--space-2)) 0 var(--space-3);
  color: var(--ink-muted);
  font-size: 13px;
  line-height: 1.4;
}

.page-meta[hidden] {
  display: none;
}

.page-chip {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
  min-height: 24px;
  min-width: 0;
  padding: 0 var(--space-2);
  overflow-wrap: anywhere;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--primary-pressed);
  background: var(--surface-muted);
  font-weight: 700;
  white-space: normal;
}

.card-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  color: var(--ink);
  font-size: 17px;
  -webkit-overflow-scrolling: touch;
}

.text-block {
  margin: 0 0 var(--space-4);
  white-space: pre-line;
  text-wrap: pretty;
}

.text-block:last-child {
  margin-bottom: 0;
}

.empty-block {
  margin: 0;
  color: var(--ink-muted);
}

.content-section {
  margin: 0;
}

.hint-section {
  margin-bottom: var(--space-4);
  padding: var(--space-3);
  border: 1px solid color-mix(in oklch, var(--primary), white 72%);
  border-radius: var(--radius);
  color: var(--primary-pressed);
  background: #f4fbf9;
}

.hint-section .text-block:last-child {
  margin-bottom: 0;
}

.table-scroll {
  width: 100%;
  max-width: 100%;
  margin-bottom: var(--space-4);
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  -webkit-overflow-scrolling: touch;
}

.answer-table {
  width: max-content;
  min-width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  font-size: 15px;
}

.answer-table th,
.answer-table td {
  padding: var(--space-3);
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
  overflow-wrap: break-word;
}

.answer-table th {
  color: var(--ink);
  background: var(--surface-muted);
  font-weight: 700;
}

.answer-table tr:last-child td {
  border-bottom: 0;
}

.answer-image-block {
  margin: 0 0 var(--space-4);
}

.image-button {
  display: block;
  width: 100%;
  min-height: 44px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: zoom-in;
}

.image-button:disabled {
  cursor: default;
}

.answer-image {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
  border-radius: calc(var(--radius) - 1px);
}

.image-fallback {
  min-height: 44px;
  padding: var(--space-3);
  color: var(--ink-muted);
  background: var(--surface-muted);
  overflow-wrap: anywhere;
  text-align: left;
}

.bottom-bar {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: var(--space-2);
  min-width: 0;
  padding: var(--space-3) var(--space-4) calc(var(--space-3) + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.search-panel[hidden],
.image-preview[hidden] {
  display: none;
}

.search-panel {
  position: fixed;
  bottom: calc(76px + env(safe-area-inset-bottom));
  right: max(var(--space-3), env(safe-area-inset-right));
  left: max(var(--space-3), env(safe-area-inset-left));
  z-index: 20;
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr);
  width: auto;
  max-width: 720px;
  height: min(70vh, 620px);
  height: min(70dvh, 620px);
  max-height: calc(100vh - 156px);
  max-height: calc(100dvh - 156px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  margin-inline: auto;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.search-head {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
  padding: var(--space-3);
  border-bottom: 1px solid var(--line);
}

.search-label {
  min-width: 0;
  font-size: 16px;
  font-weight: 700;
}

.panel-close,
.preview-close {
  min-width: 44px;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--ink);
  background: var(--surface);
  cursor: pointer;
}

.panel-close {
  min-width: 64px;
  padding: 0 var(--space-3);
  white-space: nowrap;
}

.panel-close:hover,
.preview-close:hover {
  border-color: var(--primary);
}

.search-input {
  width: auto;
  min-width: 0;
  min-height: 48px;
  margin: var(--space-3) var(--space-3) 0;
  padding: 0 var(--space-3);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--ink);
  background: var(--surface);
  font: inherit;
}

.search-summary {
  padding: var(--space-3);
  color: var(--ink-muted);
  font-size: 14px;
}

.search-results {
  display: grid;
  align-content: start;
  gap: var(--space-2);
  min-height: 0;
  padding: 0 var(--space-3) var(--space-3);
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
}

.search-result {
  display: block;
  width: 100%;
  min-width: 0;
  min-height: 48px;
  padding: 0 var(--space-3);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--ink);
  background: var(--surface);
  text-align: left;
  cursor: pointer;
}

.search-result:hover {
  border-color: var(--primary);
  background: #fbfcf9;
}

.search-result-title {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: var(--space-2);
  align-items: center;
  min-width: 0;
  min-height: 46px;
  font-weight: 700;
  font-size: clamp(12px, 3.3vw, 16px);
}

.search-result-id {
  color: var(--primary);
  white-space: nowrap;
}

.search-result-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.image-preview {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: var(--space-3);
  padding: calc(var(--space-4) + env(safe-area-inset-top)) var(--space-4) calc(var(--space-4) + env(safe-area-inset-bottom));
  background: rgba(13, 18, 16, 0.88);
}

.preview-close {
  justify-self: end;
  color: #ffffff;
  border-color: rgba(255, 255, 255, 0.28);
  background: rgba(255, 255, 255, 0.12);
}

.preview-image {
  align-self: center;
  justify-self: center;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.preview-alt {
  justify-self: center;
  max-width: min(720px, 100%);
  color: #ffffff;
  font-size: 14px;
  overflow-wrap: anywhere;
  text-align: center;
}

@media (min-width: 760px) {
  body {
    padding: var(--space-5) 0;
  }

  .app-shell {
    min-height: calc(100vh - var(--space-5) * 2);
    min-height: calc(100dvh - var(--space-5) * 2);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    overflow: hidden;
  }

  .card-title {
    font-size: 26px;
  }
}

@media (max-width: 380px) {
  .top-bar,
  .study-area,
  .bottom-bar {
    padding-left: var(--space-3);
    padding-right: var(--space-3);
  }

  .study-card {
    padding: var(--space-4);
  }

  .card-title {
    font-size: 20px;
  }

  .action-button {
    font-size: 14px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
  }
}
