:root {
    --bg: #f4efe4;
    --paper: #fffaf0;
    --paper-2: #fbf3e3;
    --ink: #302820;
    --muted: #7b6d5b;
    --line: #cdbb99;
    --brand: #6c171d;
    --brand-2: #8b2429;
    --gold: #b98a3e;
    --lu: #3c8f58;
    --quan: #7d4a9e;
    --ke: #3277b8;
    --ji: #b8423e;
    --shadow: 0 14px 40px rgba(56, 38, 20, 0.09);
}

* {
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
}

body {
    margin: 0;
    color: var(--ink);
    background:
        radial-gradient(circle at 20% 0%, rgba(185, 138, 62, 0.08), transparent 32%),
        linear-gradient(180deg, #f8f2e7 0%, var(--bg) 100%);
    font-family: "Noto Serif TC", "Microsoft JhengHei", "PingFang TC", serif;
}

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

a {
    color: inherit;
}

.site-header {
    position: sticky;
    top: 0;
    z-index: 80;
    background: rgba(255, 250, 240, 0.94);
    backdrop-filter: blur(14px);
    border-bottom: 1px solid rgba(108, 23, 29, 0.12);
}

.header-inner {
    max-width: 1500px;
    margin: 0 auto;
    padding: 12px 22px;
    display: flex;
    gap: 18px;
    align-items: center;
    justify-content: space-between;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.brand img {
    width: 160px;
    max-width: 48vw;
    height: auto;
}

.brand-subtitle {
    color: var(--muted);
    font-size: 14px;
    white-space: nowrap;
}

.header-actions,
.chart-toolbar,
.print-options-actions {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}

.btn {
    min-height: 42px;
    padding: 9px 14px;
    border: 1px solid var(--line);
    border-radius: 9px;
    background: #fffaf0;
    color: var(--ink);
    cursor: pointer;
    transition: 0.18s ease;
}

.btn:hover,
.btn:focus-visible {
    border-color: var(--brand-2);
    box-shadow: 0 0 0 3px rgba(108, 23, 29, 0.08);
    outline: 0;
}

.btn.primary {
    background: var(--brand);
    border-color: var(--brand);
    color: white;
}

.btn.is-active {
    background: #f2e4cd;
    border-color: var(--gold);
}

.page-shell {
    max-width: 1500px;
    margin: 0 auto;
    padding: 22px;
}

.intro-strip {
    margin-bottom: 16px;
    padding: 12px 15px;
    border: 1px solid rgba(185, 138, 62, 0.35);
    border-radius: 12px;
    background: rgba(255, 250, 240, 0.8);
    color: #6e5a3c;
    font-size: 14px;
}

.screen-layout {
    display: grid;
    grid-template-columns: minmax(660px, 1.52fr) minmax(360px, 0.74fr);
    gap: 18px;
    align-items: start;
}

.panel {
    border: 1px solid rgba(108, 23, 29, 0.16);
    border-radius: 14px;
    background: rgba(255, 250, 240, 0.96);
    box-shadow: var(--shadow);
    overflow: hidden;
}

.panel-head {
    padding: 12px 14px;
    background: linear-gradient(90deg, #651419, #7c2025);
    color: white;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.panel-head h1,
.panel-head h2 {
    margin: 0;
    font-size: 19px;
    letter-spacing: 0.06em;
}

.chart-toolbar {
    padding: 10px 12px;
    border-bottom: 1px solid rgba(205, 187, 153, 0.7);
    background: #fffdf7;
}

.chart-wrap {
    position: relative;
    width: 100%;
    aspect-ratio: 1 / 1;
    padding: 14px;
    background:
        linear-gradient(rgba(255, 250, 240, 0.94), rgba(255, 250, 240, 0.94)),
        repeating-linear-gradient(0deg, rgba(125, 92, 58, 0.05) 0, rgba(125, 92, 58, 0.05) 1px, transparent 1px, transparent 6px);
}

.chart-grid {
    position: relative;
    z-index: 2;
    display: grid;
    width: 100%;
    height: 100%;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    border: 1.4px solid #6e5133;
    background: var(--paper);
}

.palace,
.center-box {
    position: relative;
    min-width: 0;
    min-height: 0;
    border-right: 1px solid #9d8059;
    border-bottom: 1px solid #9d8059;
    background: rgba(255, 250, 240, 0.94);
}

.palace {
    padding: clamp(7px, 1vw, 13px);
    cursor: pointer;
    overflow: hidden;
    transition: background 0.18s ease, box-shadow 0.18s ease;
}

.palace:hover,
.palace:focus-visible {
    outline: none;
    background: #fff6e5;
    box-shadow: inset 0 0 0 2px rgba(185, 138, 62, 0.55);
}

.palace.selected {
    background: #fff2d8;
    box-shadow: inset 0 0 0 3px #b06a24;
}

.palace-top {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 6px;
    margin-bottom: 5px;
}

.palace-branch {
    font-weight: 800;
    font-size: clamp(14px, 1.3vw, 20px);
}

.palace-name {
    font-size: clamp(13px, 1.25vw, 19px);
    font-weight: 700;
}

.stars {
    display: flex;
    flex-wrap: wrap;
    gap: 3px 8px;
    margin: 8px 0 6px;
    line-height: 1.35;
}

.star-primary {
    color: var(--brand-2);
    font-size: clamp(14px, 1.3vw, 20px);
    font-weight: 800;
}

.star-minor {
    color: #4d6887;
    font-size: clamp(11px, 1vw, 15px);
}

.palace-bottom {
    position: absolute;
    left: 8px;
    right: 8px;
    bottom: 7px;
    display: flex;
    justify-content: space-between;
    color: var(--muted);
    font-size: clamp(10px, 0.82vw, 13px);
}

.self-markers {
    position: absolute;
    right: 5px;
    top: 38px;
    z-index: 5;
    display: grid;
    gap: 2px;
}

.self-marker {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 26px;
    padding: 2px 4px;
    border-radius: 999px;
    color: white;
    font-size: 10px;
    font-family: "Microsoft JhengHei", sans-serif;
    box-shadow: 0 1px 3px rgba(0,0,0,.18);
}

.self-marker.lu { background: var(--lu); }
.self-marker.quan { background: var(--quan); }
.self-marker.ke { background: var(--ke); }
.self-marker.ji { background: var(--ji); }

.center-box {
    grid-column: 2 / span 2;
    grid-row: 2 / span 2;
    padding: 20px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    background:
        radial-gradient(circle at 75% 20%, rgba(185, 138, 62, 0.09), transparent 26%),
        linear-gradient(145deg, #fffdf5, #f7edda);
}

.center-title {
    margin: 0 0 10px;
    color: var(--brand);
    font-size: clamp(23px, 2.1vw, 34px);
    letter-spacing: 0.12em;
}

.center-name {
    margin-bottom: 14px;
    font-size: clamp(18px, 1.8vw, 28px);
}

.center-meta {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 5px 12px;
    font-size: clamp(11px, 0.9vw, 14px);
}

.center-meta dt {
    color: var(--muted);
}

.center-meta dd {
    margin: 0;
}

.chart-svg {
    position: absolute;
    inset: 14px;
    z-index: 4;
    width: calc(100% - 28px);
    height: calc(100% - 28px);
    pointer-events: none;
    overflow: visible;
}

.flight-label {
    font-family: "Microsoft JhengHei", sans-serif;
    font-size: 14px;
    font-weight: 700;
    paint-order: stroke;
    stroke: #fffaf0;
    stroke-width: 5px;
    stroke-linejoin: round;
}

.legend {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    padding: 9px 12px 12px;
    color: var(--muted);
    font-size: 12px;
}

.legend span::before {
    content: "";
    width: 10px;
    height: 10px;
    border-radius: 50%;
    display: inline-block;
    margin-right: 4px;
    vertical-align: -1px;
    background: var(--dot);
}

.explanation-panel {
    position: sticky;
    top: 82px;
}

.explanation-body {
    padding: 18px;
}

.focus-title {
    display: flex;
    align-items: baseline;
    gap: 10px;
    margin: 0 0 12px;
}

.focus-title strong {
    color: var(--brand-2);
    font-size: 28px;
}

.chip-row {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 14px;
}

.chip {
    padding: 5px 8px;
    border-radius: 999px;
    background: #f1e7d2;
    border: 1px solid #ddc9a7;
    color: #665439;
    font-size: 12px;
}

.explanation-tabs {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    padding-bottom: 2px;
}

.explanation-tabs button {
    white-space: nowrap;
}

.explanation-card {
    margin-top: 14px;
    padding: 14px;
    border: 1px solid #e0cfb2;
    border-radius: 10px;
    background: #fffdf8;
    break-inside: avoid;
}

.explanation-card h3 {
    margin: 0 0 8px;
    color: var(--brand-2);
    font-size: 17px;
}

.explanation-card p,
.explanation-card li {
    line-height: 1.85;
    font-family: "Microsoft JhengHei", sans-serif;
}

.print-only {
    display: none;
}

.print-dialog {
    width: min(560px, calc(100vw - 30px));
    border: 0;
    border-radius: 14px;
    padding: 0;
    box-shadow: 0 26px 70px rgba(0,0,0,.25);
}

.print-dialog::backdrop {
    background: rgba(30, 24, 18, 0.45);
}

.print-dialog-head {
    padding: 15px 18px;
    background: var(--brand);
    color: white;
}

.print-dialog-body {
    padding: 18px;
    background: #fffaf0;
}

.option-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    margin: 14px 0 18px;
}

.option {
    display: flex;
    gap: 8px;
    align-items: flex-start;
    padding: 10px;
    border: 1px solid #ddc9a7;
    border-radius: 10px;
    background: #fffdf8;
}

.mobile-palace-sheet {
    display: none;
}

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

.admin-sidebar {
    background: #20262a;
    color: #f7f1e7;
    padding: 18px 14px;
}

.admin-sidebar .brand img {
    width: 145px;
}

.admin-nav {
    margin-top: 18px;
    display: grid;
    gap: 6px;
}

.admin-nav a {
    text-decoration: none;
    padding: 10px 12px;
    border-radius: 8px;
    color: #d9e0df;
}

.admin-nav a.active,
.admin-nav a:hover {
    background: #374148;
    color: white;
}

.admin-main {
    min-width: 0;
    background: #eef0ed;
}

.admin-topbar {
    height: 62px;
    padding: 0 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: white;
    border-bottom: 1px solid #dfe2df;
}

.admin-content {
    padding: 20px;
}

.admin-workspace {
    display: grid;
    grid-template-columns: minmax(330px, 0.75fr) minmax(420px, 1.1fr);
    gap: 16px;
}

.form-card,
.preview-card,
.editor-card {
    background: white;
    border: 1px solid #dfe2df;
    border-radius: 12px;
    padding: 16px;
}

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

.field {
    display: grid;
    gap: 5px;
}

.field.full {
    grid-column: 1 / -1;
}

.field label {
    color: #4d5b57;
    font-size: 13px;
}

.field input,
.field select,
.field textarea {
    width: 100%;
    min-height: 40px;
    border: 1px solid #cfd6d2;
    border-radius: 8px;
    padding: 8px 10px;
    background: #fff;
}

.field textarea {
    min-height: 160px;
    resize: vertical;
}

.layer-tabs {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    margin-bottom: 10px;
}

.layer-tabs .btn {
    min-height: 36px;
    padding: 7px 10px;
}

.notice {
    margin-top: 10px;
    padding: 10px 12px;
    background: #fff6de;
    border: 1px solid #ecd59f;
    border-radius: 8px;
    color: #74591d;
    font-size: 13px;
}

@media (max-width: 1120px) {
    .screen-layout {
        grid-template-columns: 1fr;
    }

    .explanation-panel {
        position: static;
    }

    .admin-workspace {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 760px) {
    .site-header {
        position: static;
    }

    .header-inner {
        padding: 10px 12px;
        align-items: flex-start;
    }

    .brand-subtitle {
        display: none;
    }

    .header-actions .btn:not(.primary) {
        display: none;
    }

    .page-shell {
        padding: 10px;
    }

    .intro-strip {
        font-size: 12px;
    }

    .panel {
        border-radius: 10px;
    }

    .panel-head {
        padding: 10px 12px;
    }

    .panel-head h1,
    .panel-head h2 {
        font-size: 16px;
    }

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

    .chart-toolbar .btn {
        width: 100%;
        min-height: 40px;
        padding: 7px;
        font-size: 13px;
    }

    .chart-wrap {
        padding: 7px;
        overflow: hidden;
    }

    .chart-svg {
        inset: 7px;
        width: calc(100% - 14px);
        height: calc(100% - 14px);
    }

    .palace {
        padding: 5px;
    }

    .palace-top {
        display: block;
        margin-bottom: 2px;
    }

    .palace-branch,
    .palace-name {
        font-size: clamp(10px, 3vw, 13px);
        line-height: 1.15;
    }

    .stars {
        margin: 4px 0 2px;
        gap: 1px 4px;
    }

    .star-primary {
        font-size: clamp(9px, 2.8vw, 12px);
    }

    .star-minor,
    .palace-bottom {
        font-size: clamp(8px, 2.1vw, 10px);
    }

    .palace-bottom {
        left: 4px;
        right: 4px;
        bottom: 3px;
    }

    .center-box {
        padding: 8px;
    }

    .center-title {
        font-size: clamp(14px, 4vw, 19px);
        margin-bottom: 5px;
    }

    .center-name {
        font-size: clamp(12px, 3.4vw, 17px);
        margin-bottom: 7px;
    }

    .center-meta {
        gap: 2px 5px;
        font-size: clamp(7px, 2vw, 9px);
    }

    .self-markers {
        right: 2px;
        top: 28px;
    }

    .self-marker {
        min-width: 20px;
        padding: 1px 3px;
        font-size: 8px;
    }

    .legend {
        font-size: 10px;
    }

    .explanation-body {
        padding: 14px;
    }

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

    .admin-shell {
        display: block;
    }

    .admin-sidebar {
        padding: 10px 12px;
    }

    .admin-nav {
        display: flex;
        overflow-x: auto;
        margin-top: 10px;
    }

    .admin-nav a {
        white-space: nowrap;
    }

    .admin-content {
        padding: 10px;
    }

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

    .field.full {
        grid-column: auto;
    }
}

@page {
    size: A4 portrait;
    margin: 8mm;
}

@media print {
    :root {
        --paper: #fff;
        --paper-2: #fff;
    }

    html,
    body {
        width: 210mm;
        min-height: 297mm;
        margin: 0;
        padding: 0;
        background: white !important;
        color: #111;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }

    .site-header,
    .intro-strip,
    .chart-toolbar,
    .legend,
    .screen-only,
    .print-dialog,
    .admin-shell {
        display: none !important;
    }

    .page-shell {
        max-width: none;
        width: 100%;
        padding: 0;
        margin: 0;
    }

    .screen-layout {
        display: block;
    }

    .panel {
        box-shadow: none;
        border: 0;
        border-radius: 0;
        background: white;
    }

    .panel-head {
        background: white;
        color: #111;
        border-bottom: 1px solid #555;
        padding: 0 0 4mm;
        margin-bottom: 3mm;
    }

    .panel-head h1,
    .panel-head h2 {
        font-size: 16pt;
    }

    .chart-panel {
        width: 190mm;
        margin: 0 auto;
        break-after: page;
        page-break-after: always;
    }

    .chart-panel > .panel-head {
        width: 190mm;
        margin: 0 auto 3mm;
    }

    .chart-wrap {
        width: 190mm;
        height: 190mm;
        aspect-ratio: 1 / 1;
        padding: 0;
        margin: 0 auto;
        background: white !important;
    }

    .chart-svg {
        inset: 0;
        width: 100%;
        height: 100%;
    }

    .palace,
    .center-box {
        background: white !important;
    }

    .palace.selected {
        background: #fff5dc !important;
        box-shadow: inset 0 0 0 1.5pt #a66821;
    }

    .flight-label {
        font-size: 10px;
        stroke-width: 3px;
        stroke: white;
    }

    .print-summary {
        display: block !important;
        width: 190mm;
        margin: 4mm auto 0;
        font-size: 9pt;
        line-height: 1.5;
    }

    .explanation-panel {
        position: static;
        width: 190mm;
        margin: 0 auto;
        break-before: page;
        page-break-before: always;
    }

    .explanation-body {
        padding: 0;
    }

    .focus-title,
    .chip-row,
    .explanation-tabs {
        break-inside: avoid;
    }

    .explanation-card {
        margin: 0 0 4mm;
        padding: 4mm;
        border: 1px solid #aaa;
        background: white;
        break-inside: avoid-page;
        page-break-inside: avoid;
    }

    .explanation-card p,
    .explanation-card li {
        font-size: 10pt;
        line-height: 1.6;
    }

    body.print-chart-only .explanation-panel {
        display: none !important;
    }

    body.print-hide-flights .chart-svg .flight-path,
    body.print-hide-flights .chart-svg .flight-label {
        display: none !important;
    }

    body.print-hide-self .self-markers {
        display: none !important;
    }
}
