/* 損益表 WebApp — 介面與報表版面
 *
 * 兩種版面共存：操作介面（可捲動、手機優先）與報表紙張（.sheet，A4 比例，
 * 螢幕預覽與列印共用同一份規則，所以預覽看到的就是印出來的）。 */

:root {
  --ink: #1b2430;
  --ink-soft: #5b6672;
  --ink-faint: #8a939e;
  --rule: #d7dce2;
  --rule-strong: #1b2430;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --accent: #1d4ed8;
  --accent-soft: #eef2ff;
  --negative: #b42318;
  --shadow: 0 1px 2px rgba(27, 36, 48, .06), 0 4px 16px rgba(27, 36, 48, .06);
  --sheet-w: 210mm;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

/* ── 頂列 ─────────────────────────────────────────────── */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--rule);
  position: sticky;
  top: 0;
  z-index: 10;
}

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

.brand-mark {
  width: 18px; height: 18px; flex: none;
  border: 2px solid var(--ink);
  border-bottom-width: 5px;
  border-radius: 2px;
}

.brand-name { font-weight: 700; letter-spacing: .08em; white-space: nowrap; }

.topbar-actions { display: flex; gap: 8px; flex: none; }

/* ── 按鈕 ─────────────────────────────────────────────── */

.btn {
  font: inherit;
  font-size: 14px;
  padding: 7px 14px;
  border: 1px solid var(--rule);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: var(--ink-faint); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: #1a43b8; border-color: #1a43b8; }
.file-btn { display: inline-flex; align-items: center; }

/* ── 抬頭欄位 ─────────────────────────────────────────── */

.meta-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--rule);
}

.field { display: flex; flex-direction: column; gap: 4px; flex: 1 1 220px; min-width: 0; }
.field-narrow { flex: 0 1 96px; }
.field-block { flex: 1 1 100%; margin-top: 14px; }
.field > span { font-size: 12px; color: var(--ink-soft); letter-spacing: .04em; }

input[type="text"], input[type="number"], textarea {
  font: inherit;
  width: 100%;
  max-width: 100%;
  padding: 7px 9px;
  border: 1px solid var(--rule);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
}
input:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { resize: vertical; }

/* ── 分頁 ─────────────────────────────────────────────── */

.tabs {
  display: flex;
  gap: 2px;
  padding: 0 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--rule);
  overflow-x: auto;
}

.tab {
  font: inherit;
  font-size: 14px;
  padding: 11px 14px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: none;
  color: var(--ink-soft);
  cursor: pointer;
  white-space: nowrap;
}
.tab:hover { color: var(--ink); }
.tab.is-active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

.status {
  margin: 0;
  padding: 0 16px;
  min-height: 0;
  font-size: 13px;
  color: var(--ink-soft);
}
.status:not(:empty) { padding: 10px 16px; background: var(--accent-soft); color: #1e3a8a; }
.status.is-error:not(:empty) { background: #fef3f2; color: var(--negative); }

main { padding: 16px; max-width: 1100px; margin: 0 auto; }
.panel[hidden] { display: none; }
.hint { font-size: 12.5px; color: var(--ink-soft); margin: 10px 2px 0; }

/* 寬表格只在自己的容器裡橫捲，整頁不被撐開 */
.table-scroll { width: 100%; overflow-x: auto; background: var(--surface); border: 1px solid var(--rule); border-radius: 8px; box-shadow: var(--shadow); }

/* ── 匯入 ─────────────────────────────────────────────── */

.dropzone {
  background: var(--surface);
  border: 2px dashed var(--rule);
  border-radius: 10px;
  padding: 32px 20px;
  text-align: center;
}
.dropzone.is-over { border-color: var(--accent); background: var(--accent-soft); }
.dropzone-title { margin: 0 0 12px; color: var(--ink-soft); }
.dropzone-hint { margin: 14px auto 0; max-width: 46em; font-size: 12.5px; color: var(--ink-faint); }

.import-summary { display: flex; flex-wrap: wrap; gap: 10px; margin: 18px 0 6px; }
.stat { flex: 1 1 120px; background: var(--surface); border: 1px solid var(--rule); border-radius: 8px; padding: 12px 14px; }
.stat-value { display: block; font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 12px; color: var(--ink-soft); }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--rule); vertical-align: middle; }
thead th { font-size: 12px; font-weight: 600; color: var(--ink-soft); letter-spacing: .04em; background: #fafbfc; position: sticky; top: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:last-child td { border-bottom: 0; }

.candidates select { font: inherit; font-size: 13px; max-width: 100%; width: 320px; padding: 5px 7px; border: 1px solid var(--rule); border-radius: 5px; }

/* ── 編輯：科目表 ─────────────────────────────────────── */

.ledger { min-width: 560px; }
.ledger .col-label { width: 42%; }
.ledger .col-amount { width: 22%; }
.ledger .col-pct { width: 12%; }
.ledger .col-src { width: 24%; }

.ledger .row-section td {
  background: #f7f9fb;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: .06em;
  color: var(--ink-soft);
  border-top: 1px solid var(--rule);
}
.ledger .row-subtotal td { background: #f7f9fb; font-weight: 600; }
.ledger .row-emphasis td { border-top: 1.5px solid var(--rule-strong); }
.ledger .row-final td { border-top: 2px solid var(--rule-strong); border-bottom: 3px double var(--rule-strong); font-size: 16px; font-weight: 700; }
.ledger .indent-1 { padding-left: 28px; }
.ledger input[type="number"] { text-align: right; }
.ledger .computed { font-variant-numeric: tabular-nums; font-weight: 600; }
.ledger .is-negative { color: var(--negative); }
.ledger .pct { font-size: 12.5px; color: var(--ink-soft); }
.ledger .src { font-size: 11.5px; color: var(--ink-faint); overflow-wrap: anywhere; }
.ledger .badge { display: inline-block; font-size: 10.5px; padding: 1px 6px; border-radius: 999px; background: var(--accent-soft); color: #1e3a8a; white-space: nowrap; }

/* ── 存檔 ─────────────────────────────────────────────── */

.archive-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.archive { min-width: 560px; }
.archive .col-ops { width: 150px; }
.archive .row-current td { background: var(--accent-soft); }
.link-btn { font: inherit; font-size: 13px; padding: 3px 8px; border: 1px solid var(--rule); border-radius: 5px; background: var(--surface); cursor: pointer; margin-right: 4px; }
.link-btn.danger { color: var(--negative); }
.empty { padding: 24px; text-align: center; color: var(--ink-faint); font-size: 13.5px; }

/* ── 預覽：報表紙張 ───────────────────────────────────── */

.preview-toolbar { display: flex; gap: 8px; justify-content: flex-end; margin-bottom: 12px; }
.sheet-scroll { width: 100%; overflow-x: auto; }

.sheet {
  width: var(--sheet-w);
  min-height: 297mm;
  margin: 0 auto;
  padding: 18mm 16mm;
  background: #fff;
  color: #000;
  box-shadow: var(--shadow);
  font-size: 10.5pt;
  line-height: 1.45;
}

.sheet-head { text-align: center; margin-bottom: 14pt; }
.sheet-company { font-size: 15pt; font-weight: 700; letter-spacing: .1em; margin: 0; }
.sheet-title { font-size: 13pt; font-weight: 700; letter-spacing: .3em; margin: 5pt 0 0; }
.sheet-period { font-size: 10pt; margin: 5pt 0 0; }
.sheet-unit { text-align: right; font-size: 9pt; margin: 10pt 0 3pt; }

table.sheet-table { width: 100%; border-collapse: collapse; font-size: 10pt; }
table.sheet-table th, table.sheet-table td { padding: 3.4pt 4pt; border: 0; }
table.sheet-table thead th { border-top: 1.2pt solid #000; border-bottom: .6pt solid #000; font-size: 9pt; font-weight: 600; }
table.sheet-table .amount { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.sheet-table .pct { text-align: right; width: 12%; font-size: 9pt; color: #444; white-space: nowrap; }
table.sheet-table .r-section td { font-weight: 700; padding-top: 7pt; }
table.sheet-table .r-item .label { padding-left: 14pt; }
table.sheet-table .r-subtotal td { border-top: .6pt solid #000; font-weight: 600; }
table.sheet-table .r-emphasis td { border-top: 1pt solid #000; font-weight: 700; }
table.sheet-table .r-final td { border-top: 1.2pt solid #000; border-bottom: 2.4pt double #000; font-weight: 700; font-size: 11pt; }
table.sheet-table .zero { color: #999; }

.sheet-note { margin-top: 12pt; font-size: 9pt; line-height: 1.5; }
.sheet-note strong { display: block; margin-bottom: 2pt; }
.sheet-foot { margin-top: 16pt; padding-top: 6pt; border-top: .6pt solid #999; font-size: 8pt; color: #555; display: flex; justify-content: space-between; gap: 10pt; }

/* ── 手機 ─────────────────────────────────────────────── */

@media (max-width: 640px) {
  body { font-size: 14.5px; }
  main { padding: 12px; }
  .topbar { padding: 9px 12px; }
  .meta-bar { padding: 10px 12px; }
  .status:not(:empty) { padding: 9px 12px; }
  /* A4 在 390px 上原尺寸會撐破畫面，縮放而不是讓整頁橫捲 */
  .sheet { transform: scale(.42); transform-origin: top left; margin-bottom: -160mm; }
  .sheet-scroll { overflow-x: hidden; }
}

/* ── 列印：只印報表 ───────────────────────────────────── */

@media print {
  @page { size: A4; margin: 12mm; }
  body { background: #fff; font-size: 10.5pt; }
  .topbar, .meta-bar, .tabs, .status, .preview-toolbar, .hint { display: none !important; }
  main { padding: 0; max-width: none; }
  .panel:not(#panel-preview) { display: none !important; }
  .sheet-scroll { overflow: visible; }
  .sheet { width: auto; min-height: 0; margin: 0; padding: 0; box-shadow: none; transform: none; }
  table.sheet-table { page-break-inside: auto; }
  table.sheet-table tr { page-break-inside: avoid; }
}
