:root {
  --paper: #f4f0e6;
  --paper-deep: #e9e1d0;
  --ink: #201d19;
  --muted: #6f675d;
  --line: #b8ad9c;
  --line-dark: #817565;
  --card: rgba(255, 253, 247, 0.94);
  --vermilion: #761c20;
  --vermilion-dark: #531316;
  --gold: #a0782d;
  --main-star: #253d72;
  --support-star: #276348;
  --malefic-star: #a13a2c;
  --other-star: #6e4c78;
  --shadow: 0 16px 44px rgba(55, 43, 26, 0.10);
  font-family: "Microsoft JhengHei", "PingFang TC", system-ui, sans-serif;
  color: var(--ink);
  background: var(--paper);
}

* { box-sizing: border-box; }

html { min-width: 320px; background: var(--paper); -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(rgba(97, 71, 39, 0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(97, 71, 39, 0.025) 1px, transparent 1px),
    var(--paper);
  background-size: 22px 22px;
}

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

.site-header,
main,
.site-footer {
  width: min(1460px, calc(100% - 48px));
  margin-inline: auto;
}

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 26px 0 20px;
  border-bottom: 1px solid var(--line);
}

.brand-lockup { display: flex; align-items: center; gap: 16px; }
.brand-lockup h1 { margin: 0; font: 700 31px/1.1 Georgia, "Noto Serif TC", serif; letter-spacing: 0.12em; }
.brand-subtitle { margin: 7px 0 0; color: var(--muted); font-size: 14px; letter-spacing: .04em; }
.eyebrow, .section-kicker { margin: 0 0 5px; color: var(--vermilion); font-size: 11px; font-weight: 800; letter-spacing: .18em; }

.brand-seal {
  display: grid;
  place-items: center;
  width: 52px;
  height: 58px;
  border: 2px solid var(--vermilion);
  color: var(--vermilion);
  font-family: Georgia, "Noto Serif TC", serif;
  font-size: 18px;
  font-weight: 800;
  line-height: 1.05;
  text-align: center;
  box-shadow: inset 0 0 0 3px var(--paper), inset 0 0 0 4px var(--vermilion);
}

.core-status {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  padding: 9px 13px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255,255,255,.45);
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #2f7953; box-shadow: 0 0 0 4px rgba(47,121,83,.12); }

main { padding: 22px 0 30px; }
.input-panel, .chart-section, .foundation-panel, .layer0-panel, .analysis-panel, .ai-export-panel {
  border: 1px solid var(--line);
  background: var(--card);
  box-shadow: var(--shadow);
}

.input-panel { padding: 21px 24px 18px; }
.section-heading, .chart-toolbar, .foundation-panel {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 28px;
}
.section-heading h2, .chart-toolbar h2, .foundation-panel h2 { margin: 0; font: 700 22px/1.2 Georgia, "Noto Serif TC", serif; letter-spacing: .04em; }
.section-note { max-width: 480px; margin: 2px 0 0; color: var(--muted); font-size: 13px; text-align: right; }

.chart-form {
  display: grid;
  grid-template-columns: minmax(180px, 1.15fr) minmax(190px, 1.25fr) minmax(145px, .8fr) minmax(145px, .8fr);
  align-items: end;
  gap: 14px;
  margin-top: 17px;
}

.field, .gender-field { min-width: 0; }
.field > span, .gender-field legend {
  display: block;
  margin-bottom: 7px;
  padding: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}
.gender-field { margin: 0; padding: 0; border: 0; }
.field input,
.field select {
  width: 100%;
  min-height: 44px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: #fffefa;
  color: var(--ink);
  outline: none;
}
.field input:focus,
.field select:focus { border-color: var(--vermilion); box-shadow: 0 0 0 3px rgba(118,28,32,.11); }
.time-24h {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 7px;
}
.time-24h > span {
  color: var(--muted);
  font-weight: 800;
  font-size: 18px;
  line-height: 1;
}

.segmented { display: grid; grid-template-columns: 1fr 1fr; min-height: 44px; border: 1px solid var(--line); border-radius: 3px; overflow: hidden; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: grid; height: 100%; place-items: center; background: #fffefa; color: var(--muted); font-weight: 700; }
.segmented label + label span { border-left: 1px solid var(--line); }
.segmented input:checked + span { background: var(--vermilion); color: white; }
.segmented input:focus-visible + span { outline: 3px solid rgba(118,28,32,.18); outline-offset: -3px; }

.form-actions { display: flex; grid-column: 1 / -1; justify-content: flex-start; gap: 8px; }
.button, .text-button {
  min-height: 44px;
  border-radius: 3px;
  font-weight: 800;
  transition: transform .15s ease, background-color .15s ease, border-color .15s ease;
}
.button:active, .text-button:active { transform: translateY(1px); }
.button { padding: 10px 18px; border: 1px solid transparent; }
.button-primary { background: var(--vermilion); color: white; }
.button-primary:hover { background: var(--vermilion-dark); }
.button-secondary { border-color: var(--line); background: transparent; color: var(--ink); }
.button-secondary:hover, .text-button:hover { border-color: var(--line-dark); background: var(--paper); }
.button:disabled { cursor: not-allowed; opacity: .55; transform: none; }
.form-status { min-height: 20px; margin-top: 9px; color: var(--muted); font-size: 12px; }
.form-status.is-error { color: var(--malefic-star); }

.saved-profile-panel {
  margin-top: 16px;
  padding-top: 15px;
  border-top: 1px solid var(--line);
}
.saved-profile-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.saved-profile-heading h3 { margin: 0; font: 700 17px/1.25 Georgia, "Noto Serif TC", serif; }
.saved-profile-count { flex: 0 0 auto; color: var(--muted); font-size: 12px; font-weight: 800; }
.saved-profile-note { margin: 7px 0 11px; color: var(--muted); font-size: 12px; line-height: 1.55; }
.saved-profile-tools { display: flex; align-items: end; justify-content: space-between; gap: 12px; margin-bottom: 9px; }
.saved-profile-search-field { flex: 1 1 320px; max-width: 440px; }
.saved-profile-range { flex: 0 0 auto; padding-bottom: 10px; color: var(--muted); font-size: 11px; font-weight: 700; }
.saved-profile-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 8px; max-height: 380px; overflow-y: auto; padding-right: 4px; overscroll-behavior: contain; }
.saved-profile-pager { display: flex; align-items: center; justify-content: center; gap: 10px; margin-top: 10px; }
.saved-profile-pager .button { min-height: 36px; padding: 7px 13px; font-size: 12px; }
.saved-profile-page-info { min-width: 86px; color: var(--muted); font-size: 12px; font-weight: 800; text-align: center; }
.saved-profile-empty { margin: 0; padding: 11px 12px; border: 1px dashed var(--line); color: var(--muted); font-size: 12px; }
.saved-profile-item { display: flex; min-width: 0; border: 1px solid var(--line); background: #fffefa; }
.saved-profile-load {
  display: flex;
  flex: 1 1 auto;
  min-width: 0;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  padding: 10px 12px;
  border: 0;
  background: transparent;
  color: var(--ink);
  text-align: left;
}
.saved-profile-load:hover { background: var(--paper); }
.saved-profile-load strong { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.saved-profile-load span { color: var(--muted); font-size: 11px; }
.saved-profile-delete { flex: 0 0 auto; padding: 0 11px; border: 0; border-left: 1px solid var(--line); background: transparent; color: var(--malefic-star); font-size: 12px; font-weight: 800; }
.saved-profile-delete:hover { background: #f8eee9; }

.chart-section { margin-top: 22px; padding: 22px 24px 18px; }
.chart-toolbar { align-items: center; }
.toolbar-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
.text-button { min-height: 36px; padding: 7px 12px; border: 1px solid var(--line); background: transparent; color: var(--ink); font-size: 13px; }

.summary-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 31px;
  margin: 17px 0 13px;
}
.summary-chip { padding: 6px 10px; border: 1px solid #d1c8b9; background: #f8f4ea; color: #544c42; font-size: 12px; }
.summary-chip strong { color: var(--ink); }

.chart-scroll { overflow: auto; border: 1px solid var(--line-dark); background: #eee7d8; overscroll-behavior-inline: contain; }
.chart-scroll:focus-visible { outline: 3px solid rgba(118,28,32,.18); outline-offset: 2px; }
.chart-grid {
  position: relative;
  display: grid;
  grid-template-columns: repeat(4, minmax(220px, 1fr));
  grid-template-rows: repeat(4, minmax(190px, 1fr));
  gap: 1px;
  min-width: 920px;
  min-height: 810px;
  background: var(--line-dark);
}

.palace-card, .chart-center { background: #fffdf6; }
.palace-card { position: relative; min-width: 0; padding: 14px 14px 39px; overflow: hidden; }
.palace-card.is-ming { background: #fbf4e6; }
.palace-card.is-body::after {
  content: "身";
  position: absolute;
  top: 11px;
  right: 11px;
  display: grid;
  width: 27px;
  height: 27px;
  place-items: center;
  border: 1px solid var(--vermilion);
  color: var(--vermilion);
  font-family: Georgia, serif;
  font-weight: 800;
}
.palace-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; padding-right: 35px; }
.palace-name { margin: 0; color: var(--vermilion); font: 800 18px/1.15 Georgia, "Noto Serif TC", serif; letter-spacing: .08em; }
.palace-branch { color: var(--muted); font-size: 12px; white-space: nowrap; }
.star-groups { display: grid; gap: 8px; margin-top: 14px; }
.star-group { display: flex; flex-wrap: wrap; gap: 5px; align-items: center; }
.star-group-label { width: 100%; color: #8b8378; font-size: 10px; font-weight: 800; letter-spacing: .14em; }
.star { display: inline-flex; align-items: center; gap: 3px; font-size: 14px; font-weight: 800; }
.star.main { color: var(--main-star); }
.star.support { color: var(--support-star); }
.star.malefic { color: var(--malefic-star); }
.star.other { color: var(--other-star); }
.transform-badge { display: inline-grid; min-width: 20px; height: 20px; place-items: center; padding: 0 4px; border: 1px solid var(--gold); border-radius: 2px; color: #7b5318; background: #fbf1d3; font-size: 10px; line-height: 1; }
.empty-main { color: #a1998e; font-size: 13px; }
.palace-footer { position: absolute; right: 12px; bottom: 11px; left: 12px; display: flex; align-items: end; justify-content: flex-start; gap: 8px; color: var(--muted); font-size: 10px; }
.palace-footer strong { display: block; color: #51483f; font-size: 11px; }

.chart-center {
  grid-column: 2 / 4;
  grid-row: 2 / 4;
  display: grid;
  grid-template-columns: auto 1fr;
  align-content: center;
  gap: 22px;
  padding: 34px;
  background:
    radial-gradient(circle at 50% 45%, rgba(160,120,45,.09), transparent 48%),
    #f8f3e8;
}
.center-mark {
  align-self: center;
  width: 84px;
  height: 96px;
  display: grid;
  place-items: center;
  border: 2px solid var(--vermilion);
  box-shadow: inset 0 0 0 5px #f8f3e8, inset 0 0 0 6px var(--vermilion);
  color: var(--vermilion);
  font: 800 25px/1.15 Georgia, "Noto Serif TC", serif;
  text-align: center;
  letter-spacing: .08em;
}
.center-content { min-width: 0; }
.center-content h3 { margin: 0 0 16px; font: 800 24px/1.2 Georgia, "Noto Serif TC", serif; }
.center-data { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 18px; margin: 0; }
.center-data div { border-top: 1px solid #cfc4b3; padding-top: 7px; }
.center-data dt { color: var(--muted); font-size: 10px; font-weight: 800; letter-spacing: .12em; }
.center-data dd { margin: 4px 0 0; font-size: 14px; font-weight: 800; }
.night-note { grid-column: 1 / -1; margin: 7px 0 0; color: var(--vermilion); font-size: 11px; }

.legend { display: flex; flex-wrap: wrap; gap: 16px; margin-top: 12px; color: var(--muted); font-size: 11px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 8px; height: 8px; border-radius: 50%; }
.legend-main { background: var(--main-star); }
.legend-support { background: var(--support-star); }
.legend-malefic { background: var(--malefic-star); }
.legend-other { background: var(--other-star); }
.legend-transform { background: var(--gold); }

.foundation-panel { margin-top: 22px; padding: 23px 24px; align-items: center; }
.foundation-panel > div { flex: 0 0 270px; }
.foundation-panel > p { max-width: 900px; margin: 0; color: var(--muted); font-size: 14px; line-height: 1.8; }

.layer0-panel { margin-top: 22px; padding: 23px 24px; }
.layer0-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 24px; }
.layer0-heading h2 { margin: 0; font: 700 22px/1.2 Georgia, "Noto Serif TC", serif; letter-spacing: .04em; }
.layer0-heading p:last-child { max-width: 780px; margin: 9px 0 0; color: var(--muted); font-size: 13px; line-height: 1.7; }
.layer0-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
.layer0-status { margin-top: 18px; padding: 11px 13px; border: 1px solid var(--line); background: #f8f4ea; color: var(--muted); font-size: 13px; font-weight: 800; }
.layer0-status.is-valid { border-color: #91b7a1; background: #edf6f0; color: #245b3e; }
.layer0-status.is-invalid { border-color: #c78e87; background: #fbefed; color: var(--vermilion); }
.layer0-metrics { display: grid; grid-template-columns: repeat(4, minmax(120px, 1fr)); gap: 10px; margin-top: 12px; }
.layer0-metrics > div { display: flex; align-items: baseline; gap: 9px; padding: 12px 14px; border: 1px solid #d4cbbb; background: #fffdf7; }
.layer0-metrics strong { color: var(--vermilion); font: 800 24px/1 Georgia, "Noto Serif TC", serif; }
.layer0-metrics span { color: var(--muted); font-size: 12px; font-weight: 700; }
.validation-details, .chain-details { margin-top: 12px; border: 1px solid var(--line); background: rgba(255,255,255,.38); }
.validation-details summary, .chain-details summary { padding: 11px 13px; color: #4e463d; font-size: 13px; font-weight: 800; cursor: pointer; }
.validation-details[open] summary, .chain-details[open] summary { border-bottom: 1px solid var(--line); }
.validation-list { display: grid; gap: 7px; margin: 0; padding: 13px; list-style: none; }
.validation-list li { display: grid; grid-template-columns: 44px 1fr; gap: 8px; font-size: 12px; line-height: 1.55; }
.validation-list b { color: #2f7953; }
.validation-list .is-failed b { color: var(--malefic-star); }
.chain-table-scroll { max-height: 510px; overflow: auto; }
.chain-table { width: 100%; border-collapse: collapse; background: #fffefa; font-size: 12px; }
.chain-table th, .chain-table td { padding: 8px 10px; border-bottom: 1px solid #ddd4c5; text-align: left; vertical-align: top; }
.chain-table th { position: sticky; top: 0; z-index: 1; background: #ece3d3; color: #4b4339; }
.chain-table td:first-child { width: 64px; color: var(--muted); font-variant-numeric: tabular-nums; }
.chain-table td:nth-child(2) { color: var(--vermilion); font-weight: 800; white-space: nowrap; }
.chain-table .is-self-chain { background: #fbf1dc; }

.analysis-panel { margin-top: 22px; padding: 23px 24px; }
.ai-export-panel { margin-top: 14px; padding: 20px 24px; }
.ai-export-controls {
  display: grid;
  grid-template-columns: 140px minmax(220px, 320px) minmax(240px, 360px) auto;
  align-items: end;
  justify-content: start;
  gap: 10px;
  margin-top: 12px;
}
.ai-export-controls .field select {
  width: 100%;
  min-height: 44px;
  padding: 9px 34px 9px 12px;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: #fffefa;
  color: var(--ink);
  font-weight: 700;
  outline: none;
}
.ai-export-controls .field select:focus { border-color: var(--vermilion); box-shadow: 0 0 0 3px rgba(118,28,32,.11); }
.ai-export-topic-field select { width: 100%; min-height: 44px; padding: 9px 34px 9px 12px; border: 1px solid var(--line); border-radius: 3px; background: #fffefa; color: var(--ink); font-weight: 700; outline: none; }
.ai-export-topic-field select:focus { border-color: var(--vermilion); box-shadow: 0 0 0 3px rgba(118,28,32,.11); }
.ai-export-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.local-ai-action-group { display: inline-flex; align-items: center; gap: 8px; }
.codex-quota-status { display: inline-flex; align-items: center; min-height: 36px; padding: 7px 10px; border: 1px solid var(--line); border-radius: 999px; background: #fffefa; color: var(--muted); font-size: 12px; font-weight: 800; white-space: nowrap; }
.codex-quota-status.is-unavailable { opacity: .7; }
.local-ai-result { margin-top: 16px; padding: 18px; border: 1px solid rgba(118, 28, 32, 0.18); border-radius: 14px; background: rgba(255, 255, 255, 0.72); }
.local-ai-result[hidden] { display: none; }
.local-ai-result-heading { margin-bottom: 12px; }
.local-ai-result-heading h3 { margin: 2px 0 0; }
.local-ai-result-body { white-space: pre-wrap; line-height: 1.8; overflow-wrap: anywhere; }
.ai-export-note { margin: 14px 0 0; padding-top: 12px; border-top: 1px dashed #d4cbbb; color: var(--muted); font-size: 12px; line-height: 1.7; }
.ai-usage-note { grid-column: 1 / -1; margin: 14px 0 0; padding: 12px 14px; border: 1px solid #d8cdbd; border-radius: 10px; background: #fffaf0; color: var(--ink); font-size: 15px; font-weight: 700; line-height: 1.8; }
.analysis-selector { display: grid; grid-template-columns: 160px minmax(0, 1fr); align-items: center; gap: 12px; margin-top: 16px; }
.analysis-selector > span { color: var(--muted); font-size: 12px; font-weight: 800; }
.analysis-selector select { min-width: 0; min-height: 44px; padding: 9px 34px 9px 12px; border: 1px solid var(--line-dark); border-radius: 3px; background: #fffefa; color: var(--ink); font-weight: 700; }
.analysis-selector select:focus { border-color: var(--vermilion); outline: 3px solid rgba(118,28,32,.11); }
.analysis-preview { margin-top: 14px; border: 1px solid var(--line-dark); background: #fffdf7; }
.analysis-preview > header { padding: 16px 18px; border-bottom: 1px solid var(--line); background: #f5efe3; }
.analysis-preview h3 { margin: 4px 0 0; color: var(--vermilion); font: 800 18px/1.4 Georgia, "Noto Serif TC", serif; }
.evidence-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.evidence-grid > section { min-width: 0; padding: 17px 18px; border-bottom: 1px solid var(--line); }
.evidence-grid > section:nth-child(odd) { border-right: 1px solid var(--line); }
.evidence-grid h4 { margin: 0 0 11px; color: #4f463d; font-size: 13px; letter-spacing: .04em; }
.evidence-item + .evidence-item { margin-top: 12px; padding-top: 12px; border-top: 1px dashed #d7cebe; }
.evidence-item strong { display: block; color: var(--vermilion); font-size: 12px; }
.evidence-item p { margin: 5px 0 0; color: #4e473f; font-size: 13px; line-height: 1.7; }
.evidence-item small { display: block; margin-top: 5px; color: #8a5d24; font-size: 11px; line-height: 1.55; }
.ai-draft { padding: 18px; border-bottom: 1px solid var(--line); background: linear-gradient(135deg, #f8f1e4, #fffdf7 70%); }
.ai-draft.is-missing { opacity: .72; }
.ai-draft-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.ai-draft-heading span { color: #7c6e5e; font-size: 11px; font-weight: 800; }
.ai-draft blockquote { margin: 16px 0; padding: 13px 17px; border-left: 4px solid var(--vermilion); background: rgba(255,255,255,.55); color: #572126; font: 800 18px/1.75 Georgia, "Noto Serif TC", serif; letter-spacing: .04em; }
.ai-explanation, .ai-verification { display: grid; grid-template-columns: 72px minmax(0, 1fr); gap: 12px; padding-top: 12px; border-top: 1px dashed #d5c9b7; }
.ai-verification { margin-top: 12px; }
.ai-explanation strong, .ai-verification strong { color: var(--vermilion); font-size: 12px; }
.ai-explanation p, .ai-verification p { margin: 0; color: #49423b; font-size: 13px; line-height: 1.75; }
.text-button:disabled { cursor: not-allowed; opacity: .45; }
.prompt-details summary { padding: 12px 18px; color: #4e463d; font-size: 13px; font-weight: 800; cursor: pointer; }
.prompt-details[open] summary { border-bottom: 1px solid var(--line); }
.prompt-details textarea { display: block; width: 100%; min-height: 430px; padding: 16px 18px; border: 0; resize: vertical; background: #fbf8f0; color: #403a34; font: 12px/1.7 Consolas, "Noto Sans TC", sans-serif; }

.site-footer { display: flex; justify-content: space-between; gap: 18px; padding: 0 0 28px; color: var(--muted); font-size: 11px; }

@media (max-width: 900px) {
  .site-header, main, .site-footer { width: min(100% - 24px, 1460px); }
  .site-header { align-items: flex-start; }
  .core-status { margin-top: 5px; }
  .chart-form { grid-template-columns: 1fr 1fr; }
  .form-actions { grid-column: 1 / -1; }
  .foundation-panel { display: block; }
  .foundation-panel > p { margin-top: 14px; }
  .layer0-heading { display: block; }
  .layer0-actions { justify-content: flex-start; margin-top: 13px; }
  .layer0-metrics { grid-template-columns: repeat(2, minmax(120px, 1fr)); }
  .evidence-grid { grid-template-columns: 1fr; }
  .evidence-grid > section:nth-child(odd) { border-right: 0; }
  .ai-export-controls { grid-template-columns: 140px minmax(0, 1fr); }
  .ai-export-controls .field-wide { grid-column: 1 / 2; }
}

@media (max-width: 620px) {
  body { padding-right: env(safe-area-inset-right); padding-left: env(safe-area-inset-left); }
  .site-header, main, .site-footer { width: calc(100% - 20px); }
  .site-header { gap: 12px; padding-top: max(16px, env(safe-area-inset-top)); padding-bottom: 14px; }
  main { padding-top: 14px; padding-bottom: 22px; }
  .brand-lockup { gap: 11px; }
  .brand-lockup h1 { font-size: 24px; letter-spacing: .08em; }
  .eyebrow { font-size: 10px; letter-spacing: .13em; }
  .brand-subtitle, .core-status { display: none; }
  .brand-seal { width: 42px; height: 47px; font-size: 15px; flex: 0 0 auto; }

  .input-panel, .chart-section, .ai-export-panel { padding: 17px 12px; }
  .input-panel, .chart-section, .ai-export-panel { box-shadow: 0 8px 24px rgba(55, 43, 26, 0.08); }
  .section-heading, .chart-toolbar, .layer0-heading { display: block; }
  .section-heading h2, .chart-toolbar h2, .layer0-heading h2 { font-size: 20px; }
  .section-note { margin-top: 8px; font-size: 12px; line-height: 1.65; text-align: left; }
  .layer0-heading p:last-child { font-size: 13px; line-height: 1.75; }

  .chart-form { grid-template-columns: 1fr; gap: 12px; margin-top: 15px; }
  .field > span, .gender-field legend { margin-bottom: 6px; font-size: 13px; }
  .field input,
  .field select,
  .ai-export-controls .field select { min-height: 50px; font-size: 16px; }
  .segmented { min-height: 50px; }
  .form-actions { display: grid; grid-template-columns: 1fr 1fr; grid-column: auto; gap: 9px; }
  .form-actions .button { width: 100%; min-height: 50px; padding-inline: 10px; }
  .form-status { min-height: 22px; margin-top: 10px; font-size: 13px; line-height: 1.6; }
  .saved-profile-tools { display: block; }
  .saved-profile-search-field { max-width: none; }
  .saved-profile-range { display: block; padding: 6px 0 0; }
  .saved-profile-list { grid-template-columns: 1fr; max-height: 340px; }
  .saved-profile-pager { justify-content: space-between; }

  .toolbar-actions { justify-content: flex-start; margin-top: 11px; }
  .text-button { min-height: 42px; }
  .summary-strip { gap: 6px; margin: 13px 0 11px; }
  .summary-chip { padding: 6px 8px; font-size: 11px; }

  .chart-scroll {
    margin-inline: -12px;
    border-right: 0;
    border-left: 0;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x pan-y;
    scrollbar-width: thin;
  }
  .chart-grid {
    grid-template-columns: repeat(4, minmax(138px, 1fr));
    grid-template-rows: repeat(4, minmax(132px, 1fr));
    min-width: 560px;
    min-height: 535px;
  }
  .palace-card { padding: 8px 8px 28px; }
  .palace-card.is-body::after { top: 7px; right: 7px; width: 22px; height: 22px; font-size: 12px; }
  .palace-heading { gap: 6px; padding-right: 25px; }
  .palace-name { font-size: 15px; }
  .palace-branch { font-size: 11px; }
  .star-groups { gap: 5px; margin-top: 8px; }
  .star-group { gap: 3px; }
  .star-group-label { font-size: 9px; }
  .star { font-size: 11px; }
  .transform-badge { min-width: 18px; height: 18px; padding-inline: 3px; font-size: 9px; }
  .palace-footer { right: 8px; bottom: 7px; left: 8px; font-size: 9px; }
  .palace-footer strong { font-size: 10px; }
  .chart-center { gap: 9px; padding: 11px; }
  .center-mark { width: 52px; height: 60px; font-size: 17px; }
  .center-content h3 { margin-bottom: 7px; font-size: 16px; }
  .center-data { gap: 5px 7px; }
  .center-data dt { font-size: 9px; }
  .center-data dd { font-size: 11px; }
  .legend { gap: 10px 13px; margin-top: 10px; }

  .foundation-panel { padding: 20px 17px; }
  .layer0-panel, .analysis-panel { padding: 20px 14px; }
  .ai-export-panel { margin-top: 12px; }
  .ai-export-controls { grid-template-columns: 1fr; gap: 11px; }
  .ai-export-controls .field-wide { grid-column: auto; }
  .ai-export-actions { display: grid; grid-template-columns: 1fr; gap: 9px; }
  .ai-export-actions .button { width: 100%; min-height: 52px; padding: 11px 14px; font-size: 15px; }
  .local-ai-action-group { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 8px; }
  .local-ai-action-group .button { width: 100%; }
  .local-ai-action-group .codex-quota-status { width: auto; max-width: 46vw; justify-content: center; white-space: normal; text-align: center; }
  .ai-usage-note { grid-column: auto; margin-top: 2px; padding: 12px; font-size: 14px; line-height: 1.75; }
  .ai-export-note { font-size: 12px; line-height: 1.75; }
  .local-ai-result { margin-top: 14px; padding: 15px 13px; border-radius: 10px; }
  .local-ai-result-heading h3 { font-size: 18px; line-height: 1.45; }
  .local-ai-result-body { font-size: 16px; line-height: 1.85; }

  .analysis-selector { grid-template-columns: 1fr; gap: 7px; }
  .ai-draft-heading { align-items: flex-start; }
  .ai-explanation, .ai-verification { grid-template-columns: 1fr; gap: 5px; }
  .layer0-metrics { grid-template-columns: 1fr 1fr; }
  .chain-table { min-width: 680px; }
  .site-footer { display: block; padding-bottom: max(24px, env(safe-area-inset-bottom)); line-height: 1.8; }
}

@media (max-width: 380px) {
  .site-header, main, .site-footer { width: calc(100% - 16px); }
  .brand-lockup h1 { font-size: 22px; }
  .input-panel, .chart-section, .ai-export-panel { padding-right: 10px; padding-left: 10px; }
  .chart-scroll { margin-inline: -10px; }
  .form-actions { grid-template-columns: 1fr; }
  .ai-usage-note { font-size: 13px; }
}

@media print {
  @page { size: A4 landscape; margin: 8mm; }
  :root { background: white; }
  body { background: white; }
  .site-header, .input-panel, .toolbar-actions, .legend, .foundation-panel, .layer0-panel, .analysis-panel, .ai-export-panel, .site-footer { display: none !important; }
  main, .chart-section { width: 100%; margin: 0; padding: 0; border: 0; box-shadow: none; }
  .chart-toolbar { margin-bottom: 7px; }
  .summary-strip { margin: 4px 0 8px; }
  .chart-scroll { overflow: visible; border-color: #444; }
  .chart-grid { min-width: 0; min-height: 178mm; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 1fr); }
  .palace-card { padding: 7px 8px 25px; }
  .palace-name { font-size: 13px; }
  .star-groups { gap: 3px; margin-top: 6px; }
  .star-group-label { display: none; }
  .star { font-size: 10px; }
  .transform-badge { min-width: 16px; height: 16px; font-size: 8px; }
  .chart-center { padding: 16px; }
  .center-mark { width: 60px; height: 70px; font-size: 18px; }
  .center-content h3 { font-size: 17px; margin-bottom: 8px; }
  .center-data { gap: 5px 10px; }
  .center-data dd { font-size: 10px; }
}
