/* 史前至夏商周學習網站 —— 預覽樣式
   視覺參考：GitHub Dashboard + Microsoft Learn，適合中一閱讀 */

:root {
  --bg: #f6f8fa;
  --bg-muted: #eef1f4;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --text: #1f2328;
  --text-muted: #57606a;
  --border: #d0d7de;
  --border-strong: #afb8c1;
  --accent: #0969da;
  --accent-hover: #0550ae;
  --accent-soft: #ddf4ff;
  --accent-text: #0550ae;
  --focus: #0969da;
  --shadow: 0 1px 2px rgba(31, 35, 40, 0.06), 0 4px 16px rgba(31, 35, 40, 0.04);
  --radius: 12px;
  --radius-sm: 8px;
  --header-h: 4.25rem;
  --read-width: 40rem;
  --font: "Noto Sans TC", "Source Han Sans TC", "PingFang TC", "Microsoft JhengHei", "Noto Sans CJK TC", sans-serif;
  --ok: #1a7f37;
  --ok-soft: #dafbe1;
  --err: #cf222e;
  --err-soft: #ffebe9;
  --warn: #9a6700;
  --warn-soft: #fff8c5;

  /* 來源層級標籤 */
  --tag-required-bg: #ddf4ff;
  --tag-required-fg: #0550ae;
  --tag-required-bd: #54aeff;
  --tag-adapted-bg: #fff8c5;
  --tag-adapted-fg: #7d4e00;
  --tag-adapted-bd: #d4a72c;
  --tag-resource-bg: #fbefff;
  --tag-resource-fg: #8250df;
  --tag-resource-bd: #c297ff;
  --tag-example-bg: #dafbe1;
  --tag-example-fg: #0d6b3d;
  --tag-example-bd: #4ac26b;
  --tag-missing-bg: #ffebe9;
  --tag-missing-fg: #a40e26;
  --tag-missing-bd: #ff8182;
}

html[data-theme="dark"] {
  --bg: #0d1117;
  --bg-muted: #010409;
  --surface: #161b22;
  --surface-soft: #1c2128;
  --text: #e6edf3;
  --text-muted: #9ea7b3;
  --border: #30363d;
  --border-strong: #484f58;
  --accent: #58a6ff;
  --accent-hover: #79b8ff;
  --accent-soft: #15233a;
  --accent-text: #79b8ff;
  --focus: #58a6ff;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.25);
  --ok: #3fb950;
  --ok-soft: #12261a;
  --err: #f85149;
  --err-soft: #2d1214;
  --warn: #d29922;
  --warn-soft: #2b2109;
  --tag-required-bg: #15233a;
  --tag-required-fg: #79b8ff;
  --tag-required-bd: #1f6feb;
  --tag-adapted-bg: #2b2109;
  --tag-adapted-fg: #e3b341;
  --tag-adapted-bd: #9a6700;
  --tag-resource-bg: #2a1a40;
  --tag-resource-fg: #d2a8ff;
  --tag-resource-bd: #8957e5;
  --tag-example-bg: #12261a;
  --tag-example-fg: #56d364;
  --tag-example-bd: #238636;
  --tag-missing-bg: #2d1214;
  --tag-missing-fg: #ff7b72;
  --tag-missing-bd: #da3633;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: var(--font);
  font-size: 1.125rem;
  line-height: 1.75;
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
}

img {
  max-width: 100%;
}

a {
  color: var(--accent);
  text-underline-offset: 0.15em;
}

a:hover {
  color: var(--accent-hover);
}

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

.skip-link {
  position: absolute;
  left: 0.75rem;
  top: -3.5rem;
  z-index: 1000;
  background: var(--accent);
  color: #fff;
  padding: 0.5rem 0.9rem;
  border-radius: var(--radius-sm);
  text-decoration: none;
  font-weight: 600;
}

.skip-link:focus {
  top: 0.75rem;
  color: #fff;
}

/* Header */
.site-header {
  position: sticky;
  top: 0;
  z-index: 200;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.header-inner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem 1rem;
  max-width: 72rem;
  margin: 0 auto;
  padding: 0.65rem 1rem;
}

.site-brand {
  display: flex;
  flex-direction: column;
  text-decoration: none;
  color: var(--text);
  min-width: 0;
  flex: 1 1 auto;
}

.site-brand:hover {
  color: var(--text);
}

.site-title {
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: 0.02em;
  line-height: 1.3;
}

.site-tagline {
  font-size: 0.8rem;
  color: var(--text-muted);
  line-height: 1.4;
  font-weight: 400;
}

.nav-toggle {
  display: none;
  margin-left: auto;
}

.site-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 0.15rem 0.15rem;
  align-items: center;
}

.site-nav a {
  display: inline-flex;
  align-items: center;
  min-height: 2.5rem;
  padding: 0.25rem 0.7rem;
  border-radius: 999px;
  text-decoration: none;
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 500;
}

.site-nav a:hover {
  background: var(--bg-muted);
  color: var(--text);
}

.site-nav a[aria-current="page"] {
  background: var(--accent-soft);
  color: var(--accent-text);
  font-weight: 600;
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-left: auto;
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  min-height: 2.5rem;
  padding: 0.35rem 0.8rem;
  border-radius: 999px;
  cursor: pointer;
  line-height: 1.2;
}

.btn:hover {
  background: var(--bg-muted);
  border-color: var(--border-strong);
}

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

.btn-primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  color: #fff;
}

.btn-ghost {
  background: transparent;
}

/* Main layout */
.site-main {
  max-width: var(--read-width);
  margin: 0 auto;
  padding: 1.75rem 1rem 3.5rem;
}

.site-main--wide {
  max-width: 48rem;
}

.hero,
.card,
.notice,
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.15rem 1.2rem 1.25rem;
  margin: 0 0 1.15rem;
}

.hero {
  padding: 1.5rem 1.3rem 1.4rem;
  background:
    linear-gradient(180deg, var(--accent-soft) 0%, var(--surface) 55%);
}

.kicker {
  display: inline-block;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: none;
  color: var(--accent-text);
  margin: 0 0 0.4rem;
}

h1, h2, h3, h4 {
  line-height: 1.35;
  font-weight: 700;
  margin: 0 0 0.7rem;
  color: var(--text);
}

h1 {
  font-size: 1.85rem;
  letter-spacing: -0.01em;
}

.notes-body > h1 {
  margin-top: 0;
}

h2 {
  font-size: 1.4rem;
  margin-top: 2rem;
  padding-top: 0.3rem;
  border-top: 1px solid var(--border);
}

.hero h1 + .lead,
.hero h1 {
  border: 0;
}

h2:first-child,
.card > h2:first-child,
#main > h2:first-of-type,
.notes-body > h2:first-child {
  border-top: 0;
  margin-top: 0;
  padding-top: 0;
}

h3 {
  font-size: 1.15rem;
  margin-top: 1.5rem;
}

h4 {
  font-size: 1.05rem;
  margin-top: 1.25rem;
}

p, ul, ol, table {
  margin: 0 0 1rem;
}

ul, ol {
  padding-left: 1.4rem;
}

li + li {
  margin-top: 0.3rem;
}

.lead {
  font-size: 1.15rem;
  color: var(--text);
}

.muted,
.meta {
  color: var(--text-muted);
  font-size: 0.98rem;
}

/* Source tags / badges — large enough on phones */
.tag,
.tag-row .tag {
  display: inline-block;
  font-size: 0.82rem;
  font-weight: 700;
  line-height: 1.4;
  padding: 0.22rem 0.55rem;
  border-radius: 999px;
  border: 1px solid;
  margin: 0 0.25rem 0.35rem 0;
  vertical-align: middle;
  max-width: 100%;
  white-space: normal;
}

.tag-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  margin: 0 0 0.8rem;
}

.tag-required {
  background: var(--tag-required-bg);
  color: var(--tag-required-fg);
  border-color: var(--tag-required-bd);
}

.tag-adapted {
  background: var(--tag-adapted-bg);
  color: var(--tag-adapted-fg);
  border-color: var(--tag-adapted-bd);
}

.tag-resource {
  background: var(--tag-resource-bg);
  color: var(--tag-resource-fg);
  border-color: var(--tag-resource-bd);
}

.tag-example {
  background: var(--tag-example-bg);
  color: var(--tag-example-fg);
  border-color: var(--tag-example-bd);
}

.tag-missing {
  background: var(--tag-missing-bg);
  color: var(--tag-missing-fg);
  border-color: var(--tag-missing-bd);
}

.tag-extension {
  background: var(--warn-soft);
  color: var(--warn);
  border: 1px solid var(--warn);
  font-weight: 700;
}

/* Tables */
.table-wrap {
  overflow-x: auto;
  margin: 0 0 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.98rem;
  margin: 0;
}

th, td {
  text-align: left;
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

th {
  background: var(--bg-muted);
  font-weight: 650;
}

tr:last-child th,
tr:last-child td {
  border-bottom: 0;
}

/* Notices */
.notice {
  background: var(--surface-soft);
}

.notice-missing {
  border-color: var(--tag-missing-bd);
  background: var(--tag-missing-bg);
}

.notice-extension {
  border-color: var(--warn);
  background: var(--warn-soft);
}

.emm-note {
  display: block;
  font-size: 0.88rem;
  color: var(--text-muted);
  margin: 0.15rem 0 0.4rem;
}

.card-grid {
  display: grid;
  gap: 0.85rem;
  margin: 0 0 1.25rem;
}

.nav-card {
  display: block;
  text-decoration: none;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem 1.1rem;
}

.nav-card:hover {
  border-color: var(--accent);
  color: var(--text);
}

.nav-card h2,
.nav-card h3 {
  margin: 0.2rem 0 0.4rem;
  font-size: 1.15rem;
  border: 0;
  padding: 0;
}

.nav-card p {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.98rem;
}

.pager {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: space-between;
  margin-top: 2rem;
}

/* Practice */
.q-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.1rem 0.9rem;
  margin: 0 0 1rem;
  box-shadow: var(--shadow);
}

.q-head {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 0.5rem;
  align-items: center;
  margin-bottom: 0.55rem;
}

.q-num {
  font-weight: 700;
}

.q-stem {
  margin: 0 0 0.8rem;
  font-weight: 500;
}

.option-list {
  list-style: none;
  padding: 0;
  margin: 0 0 0.6rem;
}

.option-list li {
  margin: 0 0 0.45rem;
}

.option-list label,
.tf-options label {
  display: flex;
  gap: 0.55rem;
  align-items: flex-start;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.7rem 0.8rem;
  min-height: 2.75rem;
  cursor: pointer;
  background: var(--surface-soft);
}

.option-list label:hover,
.tf-options label:hover {
  border-color: var(--border-strong);
}

.option-list input,
.tf-options input {
  margin-top: 0.3rem;
  width: 1.15rem;
  height: 1.15rem;
  flex: 0 0 auto;
}

.tf-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  margin: 0 0 0.7rem;
  padding: 0;
}

.fill-line {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.3rem 0.35rem;
  margin: 0 0 0.8rem;
  line-height: 2;
}

.fill-line input,
input[type="text"],
textarea {
  font-family: inherit;
  font-size: 1rem;
  line-height: 1.5;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 0.45rem 0.6rem;
  max-width: 100%;
}

.fill-line input,
input[type="text"] {
  min-height: 2.6rem;
  min-width: 7.5rem;
  width: 10rem;
}

textarea {
  width: 100%;
  min-height: 8rem;
  resize: vertical;
}

.correction-field label,
.sa-field label {
  display: block;
  font-weight: 600;
  margin: 0.2rem 0 0.35rem;
  font-size: 0.95rem;
}

.practice-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  background: transparent;
  padding: 1rem 0 0.4rem;
  margin-top: 1rem;
}

#results {
  margin: 1rem 0 1.5rem;
}

.score-summary {
  background: var(--accent-soft);
  border: 1px solid var(--accent);
}

.score-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.25rem 1rem;
  padding: 0.35rem 0;
  border-bottom: 1px dashed var(--border);
}

.score-row span:first-child {
  flex: 1 1 12rem;
  min-width: 0;
  overflow-wrap: anywhere;
}

.score-row span:last-child {
  flex: 1 1 8rem;
  text-align: right;
  overflow-wrap: anywhere;
}

.score-row:last-child,
.score-row.total {
  border-bottom: 0;
  font-weight: 700;
}

.feedback {
  margin-top: 0.75rem;
  padding: 0.8rem 0.85rem;
  border-radius: var(--radius-sm);
  background: var(--bg-muted);
  font-size: 0.98rem;
}

.feedback.ok {
  background: var(--ok-soft);
}

.feedback.bad {
  background: var(--err-soft);
}

.feedback.mixed {
  background: var(--warn-soft);
}

.feedback[hidden],
#results[hidden],
.hidden {
  display: none !important;
}

.ref-note {
  font-size: 0.9rem;
  color: var(--text-muted);
  border-left: 3px solid var(--warn);
  padding-left: 0.7rem;
}

mark.is-right,
.q-block.is-right {
  /* keep default; use feedback box */
}

body.hide-extension .extension-only {
  display: none !important;
}

.extension-hidden-banner {
  display: none;
  background: var(--warn-soft);
  border: 1px solid var(--warn);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
  margin: 0 0 1rem;
}

body.hide-extension .extension-hidden-banner {
  display: block;
}

.site-footer {
  border-top: 1px solid var(--border);
  padding: 1.25rem 1rem 2rem;
  color: var(--text-muted);
  font-size: 0.9rem;
  text-align: center;
  max-width: 48rem;
  margin: 0 auto;
}

.topic-hours {
  font-weight: 500;
  color: var(--text-muted);
  font-size: 0.95rem;
}

/* Dark mode button state */
html[data-theme="dark"] .btn-primary {
  color: #0d1117;
}

/* Mobile */
@media (max-width: 800px) {
  body {
    font-size: 1.05rem;
  }

  h1 {
    font-size: 1.55rem;
  }

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

  .site-nav {
    display: none;
    flex-basis: 100%;
    flex-direction: column;
    align-items: stretch;
    padding: 0.3rem 0 0.5rem;
  }

  .site-header.is-open .site-nav {
    display: flex;
  }

  .site-nav a {
    min-height: 2.75rem;
    border-radius: var(--radius-sm);
  }

  .header-actions {
    width: 100%;
    margin-left: 0;
  }

  .tf-options {
    grid-template-columns: 1fr;
  }

  .fill-line input,
  input[type="text"] {
    width: 100%;
  }

  th, td {
    padding: 0.55rem 0.55rem;
    font-size: 0.92rem;
  }
}

@media (min-width: 801px) {
  .card-grid.topics {
    grid-template-columns: 1fr 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
}

.practice-cta {
  margin: 1.25rem 0 0.5rem;
}

.btn-check {
  margin-top: 0.65rem;
}

.live-score {
  position: static;
}

.q-instant-hint {
  margin: 0.35rem 0 0;
  font-size: 0.9rem;
}

/* Long official URLs and tight tables on narrow screens */
.site-main,
.notes-body,
.card,
li,
p,
td,
th,
a {
  overflow-wrap: anywhere;
  word-break: break-word;
}

.table-wrap {
  max-width: 100%;
}

@media (max-width: 640px) {
  /* 兩欄「標籤／意思」表改為上下排列，避免過擠 */
  .table-wrap table:not(:has(th:nth-child(3))),
  .table-wrap table:not(:has(th:nth-child(3))) thead,
  .table-wrap table:not(:has(th:nth-child(3))) tbody,
  .table-wrap table:not(:has(th:nth-child(3))) tr,
  .table-wrap table:not(:has(th:nth-child(3))) th,
  .table-wrap table:not(:has(th:nth-child(3))) td {
    display: block;
    width: 100%;
  }

  .table-wrap table:not(:has(th:nth-child(3))) thead {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
  }

  .table-wrap table:not(:has(th:nth-child(3))) td,
  .table-wrap table:not(:has(th:nth-child(3))) th {
    border-bottom: 0;
    padding: 0.35rem 0.75rem;
  }

  .table-wrap table:not(:has(th:nth-child(3))) td:first-child,
  .table-wrap table:not(:has(th:nth-child(3))) th:first-child {
    padding-top: 0.75rem;
  }

  .table-wrap table:not(:has(th:nth-child(3))) tr {
    border-bottom: 1px solid var(--border);
  }

  .table-wrap table:not(:has(th:nth-child(3))) tr:last-child {
    border-bottom: 0;
  }

  /* 多欄表固定欄寬並允許換行，避免整頁橫向捲動 */
  .table-wrap table:has(th:nth-child(3)) {
    table-layout: fixed;
    width: 100%;
    font-size: 0.88rem;
  }

  .table-wrap table:has(th:nth-child(3)) th,
  .table-wrap table:has(th:nth-child(3)) td {
    padding: 0.45rem 0.4rem;
  }
}
