/* === Variables === */
:root {
  color-scheme: light dark;
  --page: #f5f5f7;
  --page-rgb: 245, 245, 247;
  --page-alt: #fbfbfd;
  --paper: #ffffff;
  --paper-strong: #ffffff;
  --paper-soft: #f2f2f7;
  --ink: #1d1d1f;
  --muted: #6e6e73;
  --muted-strong: #424245;
  --line: rgba(60, 60, 67, 0.16);
  --line-strong: rgba(60, 60, 67, 0.28);
  --teal: #007aff;
  --teal-dark: #005ecb;
  --mint: #e8f2ff;
  --coral: #ff3b30;
  --coral-soft: #fff0ef;
  --gold: #b7791f;
  --blue: #5e5ce6;
  --green: #34c759;
  --orange: #ff9500;
  --fill: rgba(118, 118, 128, 0.10);
  --fill-hover: rgba(118, 118, 128, 0.15);
  --fill-strong: rgba(118, 118, 128, 0.22);
  --focus: rgba(0, 122, 255, 0.23);
  --shadow: 0 18px 45px rgba(0, 0, 0, 0.07);
  --shadow-soft: 0 8px 24px rgba(0, 0, 0, 0.045);
  --placeholder: rgba(60, 60, 67, 0.48);
  --toast-bg: rgba(29, 29, 31, 0.88);
  --paper-warm: #faf8f5;
  --paper-warm-strong: #f5f0ea;
  --tech-surface: #f0f4f8;
  --tech-surface-strong: #e8eef4;
  --chart-1: #2563EB;
  --chart-2: #7C3AED;
  --chart-3: #059669;
  --chart-4: #D97706;
  --chart-5: #DC2626;
  --chart-6: #0891B2;
  --chart-area: rgba(0, 122, 255, 0.08);
  --chart-grid: rgba(60, 60, 67, 0.12);
  --heat-0: var(--fill);
  --heat-1: #bbf7d0;
  --heat-2: #86efac;
  --heat-3: #fde68a;
  --heat-4: #fca5a5;
  --wizard-done: #059669;
  --wizard-active: var(--teal);
  --wizard-pending: var(--fill);
  --sidebar-w: 220px;
}

/* === Reset === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body { font-family: -apple-system, "SF Pro SC", "PingFang SC", "Noto Sans SC", sans-serif; background: var(--page); color: var(--ink); line-height: 1.6; min-height: 100dvh; display: flex; scrollbar-width: none; -ms-overflow-style: none; }
body::-webkit-scrollbar { display: none; }
button { cursor: pointer; font: inherit; border: none; background: none; color: inherit; }
input, select, textarea { font: inherit; color: inherit; }
a { color: var(--teal); text-decoration: none; }
img { max-width: 100%; display: block; }
.hidden { display: none !important; }

.skip-link { position: absolute; left: -999px; }
.skip-link:focus { left: 1rem; top: 1rem; background: var(--teal); color: #fff; padding: .5rem 1rem; border-radius: 8px; z-index: 9999; }

/* === Sidebar === */
.sidebar { width: var(--sidebar-w); height: 100dvh; position: fixed; left: 0; top: 0; background: var(--paper); border-right: 1px solid var(--line); display: flex; flex-direction: column; z-index: 100; scrollbar-width: none; -ms-overflow-style: none; }
.sidebar::-webkit-scrollbar { display: none; }
.sidebar-brand { display: flex; align-items: center; gap: .75rem; padding: 1.25rem 1.25rem .75rem; }
.sidebar-logo { color: var(--teal); flex-shrink: 0; }
.sidebar-title { font-size: 1.15rem; font-weight: 700; letter-spacing: .03em; color: var(--ink); }
.sidebar-nav { flex: 1; padding: .5rem .75rem; display: flex; flex-direction: column; gap: .25rem; overflow-y: auto; scrollbar-width: none; -ms-overflow-style: none; }
.sidebar-nav::-webkit-scrollbar { display: none; }
.sidebar-link { display: flex; align-items: center; gap: .65rem; padding: .6rem .75rem; border-radius: 10px; font-size: .9rem; font-weight: 500; color: var(--muted); transition: background .2s, color .2s; }
.sidebar-link:hover { background: var(--fill); color: var(--ink); }
.sidebar-link.active { background: var(--mint); color: var(--teal); font-weight: 600; }
.sidebar-link svg { flex-shrink: 0; }
.sidebar-footer { padding: .75rem; border-top: 1px solid var(--line); display: flex; align-items: center; gap: .25rem; }
.sidebar-icon-btn { width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border-radius: 8px; font-size: 1.1rem; color: var(--muted); transition: background .2s, color .2s; }
.sidebar-icon-btn:hover { background: var(--fill); color: var(--ink); }

/* Theme toggle icons — JS-driven via .is-active */
.theme-icon { flex-shrink: 0; display: none; }
.theme-icon.is-active { display: block; }
#themeToggleButton .theme-icon { transition: transform .25s ease; }
#themeToggleButton:hover .theme-icon.is-active { transform: rotate(15deg); }
.theme-toggle-mobile { display: none; }

/* Mobile bar */
.mobile-bar { display: none; }
.main-content { flex: 1; margin-left: var(--sidebar-w); min-height: 100dvh; scrollbar-width: none; -ms-overflow-style: none; }
.main-content::-webkit-scrollbar { display: none; }

/* === Page layout === */
.page { display: none; height: 100dvh; }
.page.active { display: flex; }
.page-templates { display: none; }
.page-templates.active { display: grid; grid-template-columns: 240px 1fr 680px; }
.page-errors { display: none; }
.page-errors.active { display: grid; grid-template-columns: 400px 1fr; }
.page-analytics { display: none; flex-direction: column; overflow-y: auto; scrollbar-width: none; -ms-overflow-style: none; }
.page-analytics::-webkit-scrollbar { display: none; }
.page-analytics.active { display: flex; }

/* === Template list col === */
.tmpl-list-col { border-right: 1px solid var(--line); background: var(--paper); display: flex; flex-direction: column; overflow: hidden; }
.tmpl-list-head { padding: .75rem; border-bottom: 1px solid var(--line); display: flex; gap: .5rem; align-items: center; }
.tmpl-list { flex: 1; overflow-y: auto; padding: .25rem; }

/* Template editor col */
.tmpl-editor-col { display: flex; flex-direction: column; background: var(--paper-warm); overflow-y: auto; }
.tmpl-editor-head { padding: 1rem 1.25rem .75rem; display: flex; justify-content: space-between; align-items: flex-start; position: sticky; top: 0; background: var(--paper-warm); z-index: 10; }
.tmpl-editor-meta h2 { font-size: 1.1rem; font-weight: 700; }
.save-hint { font-size: .78rem; color: var(--muted); margin-top: .15rem; }

/* Template preview col */
.tmpl-preview-col { border-left: 1px solid var(--line); background: var(--paper); display: flex; flex-direction: column; overflow: hidden; }
.tmpl-preview-head { padding: 1rem 1.25rem .5rem; border-bottom: 1px solid var(--line); }
.tmpl-preview-head h2 { font-size: 1rem; font-weight: 700; }
.md-preview { flex: 1; overflow-y: auto; padding: 1rem 1.25rem; font-size: .88rem; line-height: 1.7; }

/* === Wizard === */
.wizard-bar { display: flex; align-items: center; gap: .35rem; padding: .5rem 1.25rem; }
.wiz-step { display: flex; align-items: center; gap: .35rem; padding: .35rem .7rem; border-radius: 8px; font-size: .82rem; font-weight: 500; color: var(--muted); transition: background .2s, color .2s; }
.wiz-step.active { background: var(--mint); color: var(--teal); font-weight: 600; }
.wiz-step.done { color: var(--wizard-done); }
.wiz-dot { width: 22px; height: 22px; border-radius: 50%; background: var(--fill); display: flex; align-items: center; justify-content: center; font-size: .72rem; font-weight: 700; color: var(--muted); }
.wiz-step.active .wiz-dot { background: var(--teal); color: #fff; }
.wiz-step.done .wiz-dot { background: var(--wizard-done); color: #fff; }
.wiz-line { flex: 1; height: 1px; background: var(--line); min-width: 16px; }
.wiz-panel { display: none; }
.wiz-panel.active { display: block; }

/* === Template form === */
.tmpl-form { padding: 0 1.25rem 1.25rem; }
.tmpl-basics { display: grid; grid-template-columns: repeat(3, 1fr); gap: .75rem; margin-bottom: 1rem; }
.tmpl-tags-row { margin-bottom: 1rem; max-width: 420px; }
.tmpl-field { display: flex; flex-direction: column; gap: .3rem; font-size: .82rem; font-weight: 500; color: var(--muted-strong); }
.tmpl-field input, .tmpl-field select, .tmpl-field textarea { padding: .55rem .7rem; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); font-size: .85rem; transition: border-color .2s, box-shadow .2s; }
.tmpl-field input:focus, .tmpl-field select:focus, .tmpl-field textarea:focus { outline: none; border-color: var(--teal); box-shadow: 0 0 0 3px var(--focus); }
.tmpl-field textarea { resize: vertical; line-height: 1.5; }

/* === Error list col === */
.err-list-col { border-right: 1px solid var(--line); background: var(--paper); display: flex; flex-direction: column; overflow: hidden; }
.err-list-head { padding: .75rem; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: .5rem; }
.err-filter-row { display: flex; gap: .5rem; }
.err-filter-row select, .err-filter-row input { flex: 1; padding: .45rem .6rem; border: 1px solid var(--line); border-radius: 8px; font-size: .82rem; background: var(--paper); }
.err-list { flex: 1; overflow-y: auto; padding: .25rem; }
.err-group-hdr { display: flex; align-items: center; gap: .4rem; padding: .55rem .65rem; cursor: pointer; border-radius: 8px; transition: background .15s; user-select: none; position: sticky; top: 0; background: var(--paper); z-index: 2; }
.err-group-hdr:hover { background: var(--fill); }
.err-group-arrow { font-size: .65rem; color: var(--muted); transition: transform .2s ease; flex-shrink: 0; }
.err-group-arrow.collapsed { transform: rotate(-90deg); }
.err-group-name { font-size: .82rem; font-weight: 600; color: var(--ink); flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.err-group-count { font-size: .7rem; color: var(--muted); flex-shrink: 0; }
.err-group-body { overflow: hidden; transition: max-height .25s ease; }
.err-group-body.collapsed { max-height: 0 !important; }

/* Error form col */
.err-form-col { display: flex; flex-direction: column; background: var(--paper-warm); overflow-y: auto; }
.err-form-head { padding: 1rem 1.25rem .75rem; display: flex; justify-content: space-between; align-items: flex-start; position: sticky; top: 0; background: var(--paper-warm); z-index: 10; }
.err-form-head h2 { font-size: 1.1rem; font-weight: 700; }
.err-form-head-left { display: flex; align-items: center; gap: .5rem; }
.err-back-btn { display: none; width: 36px; height: 36px; align-items: center; justify-content: center; border-radius: 8px; color: var(--muted); flex-shrink: 0; }
.err-back-btn:hover { background: var(--fill); color: var(--ink); }
.err-form { padding: 0 1.25rem 1.25rem; }
.err-form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.err-field { display: flex; flex-direction: column; gap: .3rem; font-size: .82rem; font-weight: 500; color: var(--muted-strong); }
.err-field.wide { grid-column: 1 / -1; }
.err-field input, .err-field select, .err-field textarea { padding: .55rem .7rem; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); font-size: .85rem; transition: border-color .2s, box-shadow .2s; }
.err-field input:focus, .err-field select:focus, .err-field textarea:focus { outline: none; border-color: var(--teal); box-shadow: 0 0 0 3px var(--focus); }
.err-field textarea { resize: vertical; line-height: 1.5; }

/* Date compact row */
.date-compact-row { display: flex; align-items: center; gap: .5rem; }
.date-display-text { font-size: .88rem; font-weight: 600; color: var(--ink); }
.date-parts { display: flex; gap: .35rem; }
.date-parts select { padding: .4rem .5rem; border: 1px solid var(--line); border-radius: 6px; font-size: .8rem; background: var(--paper); }

/* === Analytics === */
.analytics-hdr { padding: 1.25rem 1.5rem .75rem; background: var(--paper); border-bottom: 1px solid var(--line); }
.analytics-hdr-row { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.analytics-title { font-size: 1.1rem; font-weight: 700; margin-right: auto; color: var(--ink); }
.analytics-chips { flex-shrink: 0; }
.analytics-ctrls { display: flex; gap: .4rem; align-items: center; flex-wrap: wrap; }
.analytics-ctrls select, .analytics-ctrls input { padding: .4rem .55rem; border: 1px solid var(--line); border-radius: 8px; font-size: .8rem; background: var(--paper); min-width: 0; }
.analytics-ctrls input { width: 100px; }
.analytics-reset-btn { padding: .35rem .65rem; font-size: .78rem; }

/* Stat row */
.stat-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: .85rem; padding: 1.15rem 1.5rem; }
.stat-card { background: var(--paper); border-radius: 12px; padding: 1rem 1.1rem; position: relative; overflow: hidden; border: 1px solid var(--line); }
.stat-card::before { content: ''; position: absolute; left: 0; top: 10%; bottom: 10%; width: 3px; background: var(--stat-accent, var(--teal)); border-radius: 0 3px 3px 0; }
.stat-card-icon { display: flex; align-items: center; justify-content: center; width: 30px; height: 30px; border-radius: 8px; margin-bottom: .6rem; color: var(--stat-accent, var(--teal)); }
.stat-card-label { display: block; font-size: .72rem; font-weight: 500; color: var(--muted); letter-spacing: .02em; text-transform: uppercase; }
.stat-card-value { display: block; font-size: 1.75rem; font-weight: 800; color: var(--ink); letter-spacing: -.02em; margin-top: .15rem; line-height: 1.1; }
.stat-card-hint { display: block; font-size: .7rem; color: var(--muted); margin-top: .3rem; }
.stat-card-hero .stat-card-value { font-size: 2.6rem; }

/* Dashboard grid */
.dashboard-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; padding: 0 1.5rem 1.5rem; }
.dash-card { background: var(--tech-surface); border-radius: 14px; padding: 1.15rem 1.35rem 1.35rem; border: 1px solid var(--line); }
.dash-card-hdr { display: flex; align-items: center; gap: .7rem; margin-bottom: 1rem; }
.dash-card-hdr h2 { font-size: .92rem; font-weight: 700; color: var(--ink); }
.dash-card-hdr p { font-size: .72rem; color: var(--muted); margin-top: .1rem; }
.dash-card-icon { width: 34px; height: 34px; border-radius: 10px; background: var(--paper); display: flex; align-items: center; justify-content: center; color: var(--teal); flex-shrink: 0; }

/* Donut card */
.donut-card .donut-viewport { display: flex; flex-direction: column; align-items: center; }
.donut-chart-wrap { position: relative; display: flex; justify-content: center; }
.donut-svg { width: 160px; height: 160px; }
@keyframes donut-slice-draw { from { stroke-dasharray: 0 999; } }
.donut-slice { transition: stroke-dasharray .8s cubic-bezier(.4,0,.2,1); }
.donut-slice.animated { animation: donut-slice-draw .8s cubic-bezier(.4,0,.2,1) both; }
.donut-slice:hover { opacity: .8; }
.donut-total { font-size: 1.6rem; font-weight: 800; fill: var(--ink); }
.donut-total-label { font-size: .65rem; fill: var(--muted); }
.donut-leader { text-align: center; margin-top: .75rem; }
.donut-leader strong { font-size: .85rem; color: var(--ink); font-weight: 700; }
.donut-leader span { display: block; font-size: .72rem; color: var(--muted); margin-top: .15rem; }
.donut-legend-list { display: flex; flex-wrap: wrap; gap: .55rem .85rem; justify-content: center; margin-top: .75rem; padding-top: .75rem; border-top: 1px solid var(--line); }
.donut-legend-item { display: flex; align-items: center; gap: .35rem; font-size: .75rem; color: var(--muted-strong); }
.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.legend-label { font-size: .75rem; color: var(--muted-strong); }
.legend-meta { font-size: .68rem; color: var(--muted); }

/* Bar list */
.bar-list { display: flex; flex-direction: column; gap: .65rem; }
.compact-bars { margin-top: .85rem; padding-top: .85rem; border-top: 1px solid var(--line); }
.compact-bars .bar-row { min-height: 22px; }
.bar-row { display: flex; align-items: center; gap: .6rem; }
.bar-label { font-size: .76rem; color: var(--muted-strong); width: 100px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex-shrink: 0; }
.bar-track { flex: 1; height: 14px; background: var(--fill); border-radius: 7px; overflow: hidden; position: relative; }
.bar-fill { height: 100%; border-radius: 7px; min-width: 2px; display: flex; align-items: center; justify-content: flex-end; padding-right: 6px; transition: width .6s cubic-bezier(.4,0,.2,1); }
.bar-pct { font-size: .6rem; font-weight: 700; color: #fff; text-shadow: 0 1px 2px rgba(0,0,0,.3); white-space: nowrap; }
.bar-meta { font-size: .72rem; color: var(--muted); min-width: 40px; text-align: right; flex-shrink: 0; font-weight: 500; }

/* Rank card */
.rank-card .rank-bars { gap: .75rem; }
.rank-card .bar-track { height: 16px; border-radius: 8px; }
.rank-card .bar-fill { border-radius: 8px; }

/* Heatmap — GitHub contribution-graph style */
.heat-card-hdr { flex-wrap: wrap; }
.heat-nav { display: flex; align-items: center; gap: .45rem; margin-left: auto; }
.heat-nav-btn { width: 28px; height: 28px; display: flex; align-items: center; justify-content: center; border-radius: 6px; background: var(--fill); color: var(--muted-strong); font-size: .75rem; transition: background .15s, color .15s; }
.heat-nav-btn:hover { background: var(--fill-hover); color: var(--ink); }
.heat-nav-btn:disabled { opacity: .35; pointer-events: none; }
.heat-year { font-size: .82rem; font-weight: 700; color: var(--ink); min-width: 42px; text-align: center; }
.heatmap-viewport { overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: .35rem; }
.heat-tbl { border-collapse: separate; border-spacing: 4px; margin: 0; }
.heat-tbl th, .heat-tbl td { padding: 0; border: none; background: none; }
.heat-tbl td { line-height: 0; }
.heat-month { font-size: .56rem; color: var(--muted); font-weight: 500; text-align: left; white-space: nowrap; height: 16px; vertical-align: bottom; position: relative; overflow: visible; }
.heat-month span { position: absolute; left: 0; bottom: 0; white-space: nowrap; }
.heat-wk { font-size: .56rem; color: var(--muted); font-weight: 500; text-align: right; padding-right: 2px !important; vertical-align: middle; width: 28px; }
.heat-cell { display: block; border-radius: 2px; }
.heat-cell[data-date] { cursor: pointer; }
.heat-cell.level-0 { background: var(--heat-0); }
.heat-cell.level-1 { background: var(--heat-1); }
.heat-cell.level-2 { background: var(--heat-2); }
.heat-cell.level-3 { background: var(--heat-3); }
.heat-cell.level-4 { background: var(--heat-4); }
.heat-cell[data-date]:hover { transform: scale(1.25); position: relative; z-index: 1; }
.heat-cell.heat-selected { outline: 2px solid var(--teal); outline-offset: 1px; border-radius: 2px; transform: scale(1.25); position: relative; z-index: 1; }
.heat-filter-banner { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin-top: .5rem; padding: .4rem .75rem; background: var(--mint); border-radius: 8px; font-size: .78rem; color: var(--ink); }
.heat-filter-banner strong { color: var(--teal); }
.heat-filter-clear { background: none; border: 1px solid var(--teal); color: var(--teal); font-size: .72rem; font-weight: 600; padding: .2rem .55rem; border-radius: 5px; cursor: pointer; white-space: nowrap; }
.heat-filter-clear:hover { background: var(--teal); color: #fff; }
.heat-bottom { display: flex; justify-content: space-between; align-items: center; margin-top: .65rem; font-size: .62rem; color: var(--muted); }
.heat-legend { display: flex; align-items: center; gap: 4px; }
.heat-bottom .heat-cell { cursor: default; transform: none; }
.heat-tooltip { position: fixed; pointer-events: none; background: var(--ink); color: var(--page); font-size: .78rem; font-weight: 600; padding: .4rem .7rem; border-radius: 6px; white-space: nowrap; z-index: 9999; box-shadow: 0 4px 12px rgba(0,0,0,.22); opacity: 0; transition: opacity 80ms; }
.heat-tooltip.visible { opacity: 1; }

/* Repeat card */
.repeat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .55rem; }
.repeat-item { padding: .75rem .9rem; border-radius: 10px; background: var(--paper); border-left: 3px solid var(--teal); }
.repeat-item-head { display: flex; justify-content: space-between; align-items: flex-start; gap: .5rem; margin-bottom: .25rem; }
.repeat-item-type { font-size: .82rem; font-weight: 700; color: var(--ink); line-height: 1.3; }
.repeat-item-count { font-size: .72rem; font-weight: 700; color: var(--teal); background: var(--mint); padding: .15rem .45rem; border-radius: 5px; white-space: nowrap; flex-shrink: 0; }
.repeat-item-err { font-size: .75rem; color: var(--muted-strong); }
.repeat-item-dates { font-size: .68rem; color: var(--muted); margin-top: .3rem; }

/* === Chips === */
.chip-group { display: flex; gap: .35rem; flex-wrap: wrap; }
.chip { padding: .3rem .7rem; border-radius: 6px; font-size: .8rem; font-weight: 500; background: var(--fill); color: var(--muted-strong); transition: background .2s, color .2s; }
.chip:hover { background: var(--fill-hover); }
.chip.active { background: var(--teal); color: #fff; }

/* === Buttons === */
.btn-primary { padding: .5rem 1rem; border-radius: 8px; font-size: .85rem; font-weight: 600; background: var(--teal); color: #fff; transition: background .2s, opacity .2s; }
.btn-primary:hover { background: var(--teal-dark); }
.btn-danger { padding: .5rem 1rem; border-radius: 8px; font-size: .85rem; font-weight: 600; background: var(--coral); color: #fff; transition: background .2s, opacity .2s; }
.btn-danger:hover { opacity: .85; }
.btn-ghost { padding: .5rem 1rem; border-radius: 8px; font-size: .85rem; font-weight: 500; color: var(--teal); transition: background .2s; }
.btn-ghost:hover { background: var(--fill); }
.btn-icon { width: 34px; height: 34px; display: flex; align-items: center; justify-content: center; border-radius: 8px; color: var(--teal); transition: background .2s; }
.btn-icon:hover { background: var(--fill); }
.btn-row { display: flex; gap: .5rem; align-items: center; }

/* === Search field === */
.search-field { flex: 1; padding: .5rem .75rem; border: 1px solid var(--line); border-radius: 8px; font-size: .85rem; background: var(--paper); transition: border-color .2s, box-shadow .2s; }
.search-field:focus { outline: none; border-color: var(--teal); box-shadow: 0 0 0 3px var(--focus); }

/* === Inline controls === */
.inline-select-row { display: flex; gap: .35rem; align-items: center; }
.inline-select-row select { flex: 1; }
.inline-add-btn { width: 28px; height: 28px; display: flex; align-items: center; justify-content: center; border-radius: 6px; font-size: .9rem; font-weight: 700; color: var(--teal); background: var(--mint); transition: background .2s; flex-shrink: 0; }
.inline-add-btn:hover { background: var(--teal); color: #fff; }
.inline-add-btn.manage-btn { font-size: .8rem; color: var(--muted); background: var(--fill); }
.inline-add-btn.manage-btn:hover { background: var(--fill-strong); color: var(--ink); }

/* Error Type Manager Modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 500; display: flex; align-items: center; justify-content: center; padding: 1rem; }
.modal-overlay[hidden] { display: none; }
.modal-panel { background: var(--paper); border-radius: 14px; width: 100%; max-width: 380px; max-height: 70vh; display: flex; flex-direction: column; box-shadow: var(--shadow); }
.modal-hdr { display: flex; align-items: center; justify-content: space-between; padding: .85rem 1.1rem; border-bottom: 1px solid var(--line); }
.modal-hdr h3 { font-size: .95rem; font-weight: 700; color: var(--ink); }
.modal-close { width: 30px; height: 30px; display: flex; align-items: center; justify-content: center; border-radius: 8px; font-size: .85rem; color: var(--muted); transition: background .2s; }
.modal-close:hover { background: var(--fill); color: var(--ink); }
.modal-body { padding: .85rem 1.1rem 1rem; overflow-y: auto; flex: 1; }
.modal-hint { font-size: .78rem; color: var(--muted); margin-bottom: .75rem; line-height: 1.5; }
.manage-list { list-style: none; }
.manage-item { display: flex; align-items: center; gap: .5rem; padding: .5rem .4rem; border-radius: 8px; transition: background .15s; }
.manage-item:hover { background: var(--fill); }
.manage-item-name { flex: 1; font-size: .85rem; color: var(--ink); }
.manage-item-count { font-size: .72rem; color: var(--muted); white-space: nowrap; }
.manage-item-del { width: 30px; height: 30px; display: flex; align-items: center; justify-content: center; border-radius: 6px; font-size: .8rem; color: var(--muted); transition: background .15s, color .15s; flex-shrink: 0; }
.manage-item-del:hover { background: var(--coral-soft); color: var(--coral); }
@media (max-width: 767px) {
  .modal-panel { max-width: 100%; max-height: 80vh; border-radius: 12px; }
  .manage-item { padding: .6rem .4rem; }
  .manage-item-del { width: 36px; height: 36px; }
}

/* Optional toggle */
.optional-toggle { padding: .5rem 0; }
.optional-field.hidden { display: none !important; }

/* === List items === */
.item-card { padding: .6rem .75rem; border-radius: 8px; cursor: pointer; transition: background .15s; margin-bottom: .15rem; }
.item-card:hover { background: var(--fill); }
.item-card.active { background: var(--mint); }
.item-card-title { font-size: .85rem; font-weight: 600; color: var(--ink); }
.item-card-sub { font-size: .75rem; color: var(--muted); margin-top: .1rem; }


/* === Quick capture === */
.qc-overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); display: flex; align-items: center; justify-content: center; z-index: 1000; backdrop-filter: blur(4px); }
.qc-overlay[hidden] { display: none; }
.qc-card { background: var(--paper); border-radius: 16px; width: min(460px, 92vw); max-height: 90vh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2); padding: 1.5rem; }
.qc-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
.qc-head h2 { font-size: 1.05rem; font-weight: 700; }
.qc-head button { font-size: 1.1rem; color: var(--muted); }
.qc-field { margin-bottom: .75rem; }
.qc-field > span { display: block; font-size: .78rem; font-weight: 500; color: var(--muted-strong); margin-bottom: .3rem; }
.qc-field input, .qc-field select, .qc-field textarea { width: 100%; padding: .55rem .7rem; border: 1px solid var(--line); border-radius: 8px; font-size: .85rem; background: var(--paper); color: inherit; }
.qc-field input:focus, .qc-field select:focus, .qc-field textarea:focus { outline: none; border-color: var(--teal); box-shadow: 0 0 0 3px var(--focus); }
.qc-chip-row { display: flex; flex-wrap: wrap; gap: .35rem; }
.qc-chip-row .chip { cursor: pointer; }
.qc-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1rem; }

/* === App menu === */
.app-menu-wrap { position: relative; }
.app-menu { position: absolute; bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%); background: var(--paper); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); min-width: 140px; padding: .35rem; z-index: 200; }
.app-menu button { display: block; width: 100%; text-align: left; padding: .5rem .75rem; border-radius: 6px; font-size: .85rem; color: var(--ink); }
.app-menu button:hover { background: var(--fill); }
.app-menu-sep { height: 1px; background: var(--line); margin: .25rem 0; }

/* === Toast === */
.toast { position: fixed; bottom: 2rem; left: 50%; transform: translateX(-50%); background: var(--toast-bg); color: #fff; padding: .6rem 1.25rem; border-radius: 10px; font-size: .85rem; opacity: 0; pointer-events: none; transition: opacity .3s; z-index: 9999; white-space: nowrap; }
.toast.show { opacity: 1; pointer-events: auto; }

/* === Dark mode === */
[data-resolvedTheme="dark"],
.theme-dark {
  color-scheme: dark;
  --page: #000000;
  --page-rgb: 0, 0, 0;
  --page-alt: #0a0a0a;
  --paper: #1c1c1e;
  --paper-strong: #2c2c2e;
  --paper-soft: #242426;
  --paper-warm: #1a1816;
  --paper-warm-strong: #24211d;
  --tech-surface: #0f1419;
  --tech-surface-strong: #171d24;
  --ink: #f5f5f7;
  --muted: #98989d;
  --muted-strong: #aeaeb2;
  --line: rgba(255, 255, 255, 0.14);
  --line-strong: rgba(255, 255, 255, 0.24);
  --teal: #0a84ff;
  --teal-dark: #409cff;
  --mint: rgba(10, 132, 255, 0.15);
  --coral: #ff453a;
  --coral-soft: rgba(255, 69, 58, 0.15);
  --fill: rgba(118, 118, 128, 0.18);
  --fill-hover: rgba(118, 118, 128, 0.28);
  --fill-strong: rgba(118, 118, 128, 0.36);
  --focus: rgba(10, 132, 255, 0.28);
  --shadow: 0 18px 45px rgba(0, 0, 0, 0.35);
  --shadow-soft: 0 8px 24px rgba(0, 0, 0, 0.25);
  --placeholder: rgba(235, 235, 245, 0.36);
  --toast-bg: rgba(44, 44, 46, 0.92);
  --heat-0: var(--fill);
  --heat-1: rgba(34, 197, 94, 0.30);
  --heat-2: rgba(34, 197, 94, 0.50);
  --heat-3: rgba(234, 179, 8, 0.45);
  --heat-4: rgba(239, 68, 68, 0.45);
  --chart-area: rgba(10, 132, 255, 0.12);
  --chart-grid: rgba(255, 255, 255, 0.12);
}

/* === Responsive: tablet === */
@media (max-width: 1366px) {
  :root { --sidebar-w: 170px; }
  .page-templates.active { grid-template-columns: 210px 1fr 360px; }
  .page-errors.active { grid-template-columns: 380px 1fr; }
  .dashboard-grid { grid-template-columns: 1fr; gap: .85rem; }
  .stat-row { grid-template-columns: 1fr 1fr; gap: .6rem; }
  .stat-card-hero { grid-column: auto; }
  .stat-card-hero .stat-card-value { font-size: 1.5rem; }
  .repeat-grid { grid-template-columns: 1fr; }
  .bar-label { width: 80px; }
  .compact-bars .bar-row { min-height: 18px; }
  .analytics-ctrls { gap: .3rem; }
  .analytics-ctrls select, .analytics-ctrls input { padding: .35rem .45rem; font-size: .78rem; }
  .donut-svg { width: 130px; height: 130px; }
}

/* === Responsive: phone landscape / small tablet === */
@media (max-width: 768px) {
  .stat-row { grid-template-columns: 1fr 1fr; }
  .stat-card-hero { grid-column: 1 / -1; }
  .stat-card-hero .stat-card-value { font-size: 2rem; }
  .bar-label { width: 65px; font-size: .7rem; }
  .bar-meta { min-width: 32px; font-size: .68rem; }
  .donut-svg { width: 110px; height: 110px; }
  .dash-card { padding: 1rem 1.1rem; }
}

/* === Responsive: mobile === */
@media (max-width: 767px) {
  input, select, textarea { font-size: 16px !important; }
  .sidebar { display: none; }
  .main-content { margin-left: 0; padding-bottom: 72px; }
  .mobile-bar { display: flex; position: fixed; bottom: 0; left: 0; right: 0; background: var(--paper); border-top: 1px solid var(--line); z-index: 100; padding: .4rem 0 env(safe-area-inset-bottom, .4rem); }
  .mobile-bar-btn { flex: 1; display: flex; flex-direction: column; align-items: center; gap: .15rem; padding: .35rem 0; font-size: .68rem; color: var(--muted); transition: color .2s; }
  .mobile-bar-btn.active { color: var(--teal); }
  .mobile-bar-btn svg { flex-shrink: 0; }
  .theme-toggle-mobile { display: flex; }
  .theme-toggle-mobile .theme-icon-moon,
  .theme-toggle-mobile .theme-icon-sun { width: 22px; height: 22px; }
  .page { height: auto; min-height: calc(100dvh - 72px); }
  .page.active { display: flex !important; flex-direction: column; }
  .page-templates.active { display: flex !important; flex-direction: column; }
  .page-errors.active { display: flex !important; flex-direction: column; }
  .tmpl-list-col { border-right: none; border-bottom: 1px solid var(--line); max-height: 200px; }
  .tmpl-editor-col { border-bottom: 1px solid var(--line); }
  .tmpl-preview-col { border-left: none; border-top: 1px solid var(--line); max-height: 300px; }
  .tmpl-basics { grid-template-columns: 1fr; }
  .err-list-col { border-right: none; flex: 1; }
  .err-form-col { display: none !important; }
  .page-errors.show-form .err-list-col { display: none !important; }
  .page-errors.show-form .err-form-col { display: flex !important; flex: 1; }
  .err-back-btn { display: flex; }
  .err-form-grid { grid-template-columns: 1fr; }
  .err-field.wide { grid-column: 1; }
  .analytics-hdr { padding: 1rem; }
  .analytics-hdr-row { flex-direction: column; align-items: stretch; gap: .6rem; }
  .analytics-title { margin-right: 0; }
  .stat-row { grid-template-columns: 1fr 1fr; padding: .85rem 1rem; gap: .5rem; }
  .stat-card-hero { grid-column: auto; }
  .stat-card-hero .stat-card-value { font-size: 1.5rem; }
  .dashboard-grid { grid-template-columns: 1fr; padding: 0 1rem 1rem; }
  .repeat-grid { grid-template-columns: 1fr; }
  .bar-label { width: 58px; font-size: .68rem; }
  .bar-meta { min-width: 28px; font-size: .65rem; }
  .donut-svg { width: 100px; height: 100px; }
  .donut-legend-list { gap: .4rem .65rem; }
  .heat-nav-btn { width: 32px; height: 32px; }
  .heat-year { font-size: .88rem; }
  .dash-card { padding: .85rem 1rem; }
  .qc-card { margin: 1rem; width: auto; }
}

/* === Export sheet overlay === */
.export-sheet { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); display: flex; align-items: center; justify-content: center; z-index: 1000; backdrop-filter: blur(4px); }
.export-sheet textarea { width: 100%; min-height: 200px; padding: .75rem; border: 1px solid var(--line); border-radius: 8px; font-size: .82rem; font-family: ui-monospace, "SF Mono", Menlo, monospace; background: var(--paper-soft); resize: vertical; margin-top: .75rem; }

/* === Button feedback === */
@keyframes ios-button-pop { 0% { transform: scale(1); } 40% { transform: scale(0.92); } 100% { transform: scale(1); } }
.tap-pop { animation: ios-button-pop .18s ease-out; }
button.is-pressing { opacity: .7; }

/* === Print === */
@media print {
  .sidebar, .mobile-bar, .qc-overlay, .toast { display: none !important; }
  .main-content { margin-left: 0; }
  .page { height: auto; }
  body { background: #fff; }
}

/* === Markdown preview === */
.md-preview h1, .md-preview h2, .md-preview h3 { margin: 1em 0 .5em; font-weight: 700; }
.md-preview h1 { font-size: 1.25rem; }
.md-preview h2 { font-size: 1.1rem; }
.md-preview h3 { font-size: 1rem; }
.md-preview p { margin-bottom: .75em; }
.md-preview ul, .md-preview ol { margin-left: 1.5em; margin-bottom: .75em; }
.md-preview li { margin-bottom: .25em; }
.md-preview code { font-size: .88em; background: var(--fill); padding: .1em .3em; border-radius: 4px; }
.md-preview pre { background: var(--fill); padding: 1rem; border-radius: 8px; overflow-x: auto; margin-bottom: 1em; }
.md-preview pre code { background: none; padding: 0; }
.md-preview blockquote { border-left: 3px solid var(--teal); padding-left: 1em; color: var(--muted-strong); margin-bottom: 1em; }
.md-preview strong { font-weight: 700; }
.md-preview em { font-style: italic; }
.md-preview table { width: 100%; border-collapse: collapse; margin-bottom: 1em; }
.md-preview th, .md-preview td { border: 1px solid var(--line); padding: .4em .6em; text-align: left; }
.md-preview th { background: var(--fill); font-weight: 600; }

/* === List items === */
.list-item { padding: .6rem .75rem; border-radius: 8px; cursor: pointer; transition: background .15s; margin-bottom: .15rem; }
.list-item:hover { background: var(--fill); }
.list-item.active { background: var(--mint); }
.list-item-title { font-size: .85rem; font-weight: 600; color: var(--ink); line-height: 1.4; }
.list-item p { font-size: .75rem; color: var(--muted); margin-top: .1rem; }
.list-item-meta { font-size: .75rem; color: var(--muted); }
.list-count { font-size: .72rem; color: var(--muted); padding: .5rem .75rem; text-align: center; }
.list-load-more { width: 100%; margin-top: .5rem; }

/* === Tags === */
.tag { display: inline-block; padding: .1rem .4rem; background: var(--fill); border-radius: 4px; font-size: .7rem; color: var(--muted-strong); }
.tag-line { display: flex; flex-wrap: wrap; gap: .25rem; margin-top: .25rem; }

/* === Empty state === */
.empty-state { text-align: center; padding: 2rem 1rem; color: var(--muted); font-size: .88rem; }

/* === Export card === */
.export-card { background: var(--paper); border-radius: 16px; padding: 1.5rem; width: min(600px, 92vw); max-height: 80vh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,.2); }
.export-card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
.export-actions { margin-top: 1rem; display: flex; justify-content: flex-end; gap: .5rem; }

/* === Chart summary === */
.chart-summary { font-size: .82rem; color: var(--muted-strong); padding: .5rem 0; }

/* === Quick chips === */
.quick-chip { padding: .3rem .6rem; border-radius: 6px; font-size: .8rem; background: var(--fill); color: var(--muted-strong); cursor: pointer; transition: background .2s; }
.quick-chip:hover { background: var(--fill-hover); }
.quick-chip.selected { background: var(--teal); color: #fff; }

/* === Preview trap record === */
.preview-trap-record {
  display: inline-block;
  margin-left: .5rem;
  padding: .1rem .5rem;
  font-size: .75rem;
  line-height: 1.4;
  color: var(--teal);
  background: transparent;
  border: 1px solid var(--teal);
  border-radius: 3px;
  cursor: pointer;
  vertical-align: middle;
  transition: background .15s, color .15s;
}
.preview-trap-record:hover { background: var(--teal); color: #fff; }
.preview-trap-record[data-record-field="safeguard"] { color: var(--muted); border-color: var(--muted); }
.preview-trap-record[data-record-field="safeguard"]:hover { background: var(--muted); color: #fff; }

/* === Math display === */
.math-display { font-size: .88rem; line-height: 1.6; }

/* === Trend SVG === */
.trend-svg { width: 100%; height: 40px; }

/* === Axis label === */
.axis-label { font-size: .7rem; color: var(--muted); }
