/* 研究經費帳本
   色彩取自帳簿與核章：帳簿綠＝錢已落定，印泥紅＝錢還掛在外面，鉛筆灰＝只是預留。 */
:root {
  --paper: #f3f5f1;
  --sheet: #ffffff;
  --ink: #17231f;
  --ink-2: #53615c;
  --ink-3: #8a9591;
  --rule: #d6dcd6;
  --rule-soft: #e7ebe6;
  --green: #1c6b57;
  --green-deep: #16302b;
  --green-wash: #e3efe9;
  --seal: #b23a26;
  --seal-wash: #f8e8e3;
  --pencil: #9aa39f;
  --amber: #8a6410;
  --amber-wash: #f5ecd3;
  --radius: 6px;
  --font: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.6 var(--font);
  font-variant-numeric: tabular-nums;
}
[hidden] { display: none !important; }
h1, h2, h3, p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--green); outline-offset: 2px; }

/* ── 按鈕 ── */
.btn {
  border: 1px solid var(--rule);
  background: var(--sheet);
  border-radius: var(--radius);
  padding: 7px 14px;
  cursor: pointer;
  white-space: nowrap;
  line-height: 1.4;
}
.btn:hover { border-color: var(--ink-3); }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; font-weight: 600; }
.btn.primary:hover { background: #175a49; }
.btn.small { padding: 4px 10px; font-size: 14px; }
.btn.danger { color: var(--seal); border-color: #e2bdb4; }
.btn.step { border-color: var(--green); color: var(--green); font-weight: 600; }
.link-btn { border: 0; background: none; padding: 4px 2px; cursor: pointer; color: inherit; text-decoration: underline; text-underline-offset: 3px; text-align: left; }

/* ── 登入 ── */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; background: var(--green-deep); }
.login-card { width: min(380px, 100%); background: var(--sheet); border-radius: 10px; padding: 28px 24px; display: grid; gap: 14px; }
.login-card h1 { font-size: 24px; letter-spacing: .04em; }
.login-lead { color: var(--ink-2); font-size: 15px; }
.who-list { display: grid; gap: 10px; }
.who-list button { border: 1px solid var(--rule); background: var(--sheet); border-radius: var(--radius); padding: 16px; font-size: 20px; font-weight: 600; cursor: pointer; text-align: left; }
.who-list button:hover { border-color: var(--green); background: var(--green-wash); }

label { display: grid; gap: 4px; font-size: 14px; color: var(--ink-2); }
input, select, textarea {
  width: 100%;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 9px 10px;
  background: #fff;
  font-size: 16px;
  color: var(--ink);
}
textarea { min-height: 64px; resize: vertical; }
.hint { font-size: 13px; color: var(--ink-2); }
.form-error { color: var(--seal); font-size: 14px; min-height: 1.2em; }

/* ── 頂列 ── */
.top { background: var(--green-deep); color: #fff; position: sticky; top: 0; z-index: 5; }
.top-inner { max-width: 1120px; margin: 0 auto; padding: 0 16px; display: flex; align-items: center; gap: 20px; min-height: 52px; }
.brand { font-weight: 700; letter-spacing: .06em; white-space: nowrap; }
.tabs { display: flex; gap: 2px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.tabs button {
  border: 0; background: none; color: #c5d6d0; padding: 15px 12px; cursor: pointer; white-space: nowrap;
  border-bottom: 3px solid transparent; font-size: 15px;
}
.tabs button[aria-current="page"] { color: #fff; border-bottom-color: #8fd3bd; font-weight: 600; }
.who { font-size: 14px; white-space: nowrap; color: #d9e6e1; }

main { max-width: 1120px; margin: 0 auto; padding: 24px 16px 80px; outline: none; }

/* ── 總覽：一句話＋數字 ── */
.standing { display: grid; grid-template-columns: 1fr auto; gap: 8px 24px; align-items: end; margin-bottom: 20px; }
.standing-main p:first-child { color: var(--ink-2); font-size: 15px; }
.standing-num { font-size: clamp(38px, 8vw, 56px); font-weight: 700; line-height: 1.1; letter-spacing: -.01em; }
.standing-num small { font-size: .42em; font-weight: 500; color: var(--ink-2); margin-right: 6px; }
.standing-sub { color: var(--ink-2); font-size: 15px; margin-top: 6px; max-width: 46em; }
.standing-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.chips { display: flex; gap: 6px; flex-wrap: wrap; margin: 0 0 18px; }
.chip { border: 1px solid var(--rule); background: var(--sheet); border-radius: 99px; padding: 4px 14px; cursor: pointer; font-size: 14px; }
.chip[aria-pressed="true"] { background: var(--ink); color: #fff; border-color: var(--ink); }

/* ── 要處理的事 ── */
.todo { background: var(--sheet); border: 1px solid var(--rule); border-left: 4px solid var(--seal); border-radius: var(--radius); margin-bottom: 28px; }
.todo.clear { border-left-color: var(--green); }
.todo h2 { font-size: 15px; padding: 12px 16px 0; }
.todo ul { list-style: none; margin: 0; padding: 6px 0 8px; }
.todo li { display: flex; gap: 12px; align-items: baseline; justify-content: space-between; padding: 7px 16px; border-top: 1px solid var(--rule-soft); }
.todo li:first-child { border-top: 0; }
.todo .amt { font-weight: 700; white-space: nowrap; }
.todo .empty { color: var(--ink-2); padding: 4px 16px 12px; font-size: 15px; }

/* ── 計畫 ── */
.project { background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius); margin-bottom: 20px; }
.project.dim { opacity: .72; }
.project-head { display: flex; gap: 12px; justify-content: space-between; align-items: flex-start; padding: 16px 16px 12px; flex-wrap: wrap; }
.project-head h2 { font-size: 19px; line-height: 1.35; }
.project-meta { color: var(--ink-2); font-size: 14px; margin-top: 2px; }
.project-note { color: var(--ink-2); font-size: 13px; padding: 0 16px 12px; max-width: 60em; }
.project-tools { display: flex; gap: 6px; flex-wrap: wrap; }
.tag { display: inline-block; font-size: 12.5px; border-radius: 4px; padding: 1px 8px; border: 1px solid var(--rule); color: var(--ink-2); white-space: nowrap; vertical-align: middle; }
.tag.kind { background: var(--green-deep); border-color: var(--green-deep); color: #fff; }
.tag.warn { background: var(--seal-wash); border-color: #e2bdb4; color: var(--seal); }
.tag.planned { background: #f0f2f0; color: var(--ink-2); border-style: dashed; border-color: var(--pencil); }
.tag.pending { background: var(--seal-wash); border-color: #e2bdb4; color: var(--seal); }
.tag.submitted { background: var(--amber-wash); border-color: #dcc88f; color: var(--amber); }
.tag.settled { background: var(--green-wash); border-color: #b5d3c7; color: var(--green); }

.item { display: grid; grid-template-columns: minmax(120px, 1.1fr) minmax(200px, 3fr) auto; gap: 6px 20px; align-items: center; padding: 12px 16px; border-top: 1px solid var(--rule-soft); }
.item-name { font-weight: 600; }
.item-name small { display: block; font-weight: 400; color: var(--ink-2); font-size: 13px; }
.item-left { text-align: right; white-space: nowrap; }
.item-left b { font-size: 20px; display: block; line-height: 1.2; }
.item-left small { color: var(--ink-2); font-size: 13px; }
.item-left.over b { color: var(--seal); }
.item-tools { grid-column: 1 / -1; display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.item-figs { font-size: 13px; color: var(--ink-2); margin-top: 4px; display: flex; flex-wrap: wrap; gap: 2px 14px; }
.item-figs i { font-style: normal; display: inline-flex; align-items: center; gap: 5px; }

/* 這一條是整個網站的重點：一個科目的錢走到哪一步 */
.bar { display: flex; height: 18px; border-radius: 3px; overflow: hidden; background: #eef1ed; box-shadow: inset 0 0 0 1px var(--rule); }
.bar span { display: block; min-width: 0; }
.seg-settled, .key-settled { background: var(--green); }
.seg-submitted, .key-submitted { background: repeating-linear-gradient(135deg, var(--green) 0 4px, #8fc4b3 4px 8px); }
.seg-pending, .key-pending { background: var(--seal); }
.seg-planned, .key-planned { background: repeating-linear-gradient(90deg, var(--pencil) 0 3px, transparent 3px 6px); }
.key { width: 12px; height: 12px; border-radius: 2px; display: inline-block; flex: none; box-shadow: inset 0 0 0 1px rgba(0,0,0,.08); }
.legend { display: flex; flex-wrap: wrap; gap: 4px 18px; font-size: 13px; color: var(--ink-2); margin: 0 0 14px; }
.legend i { font-style: normal; display: inline-flex; align-items: center; gap: 6px; }

.empty-box { background: var(--sheet); border: 1px dashed var(--pencil); border-radius: var(--radius); padding: 28px 20px; text-align: center; color: var(--ink-2); }
.empty-box .btn { margin-top: 12px; }

/* ── 表格 ── */
.section-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.section-head h2 { font-size: 22px; }
.section-head p { color: var(--ink-2); font-size: 14px; max-width: 46em; }
.filters { display: grid; grid-template-columns: 2fr repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.table-wrap { background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius); overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 15px; }
th { text-align: left; font-weight: 600; font-size: 13px; color: var(--ink-2); padding: 9px 12px; border-bottom: 1px solid var(--rule); white-space: nowrap; background: #fafbf9; }
td { padding: 10px 12px; border-bottom: 1px solid var(--rule-soft); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; white-space: nowrap; }
td.keep { white-space: nowrap; }
td.state { min-width: 9.5em; }
td.num b { font-weight: 700; }
td small, .sub { display: block; color: var(--ink-2); font-size: 13px; }
td.actions { white-space: nowrap; text-align: right; }
td.actions .btn { margin-left: 4px; }
tfoot td { background: #fafbf9; font-weight: 600; border-top: 1px solid var(--rule); }
.age-late { color: var(--seal); font-weight: 600; }
.diff-zero { color: var(--green); font-weight: 600; }
.diff-bad { color: var(--seal); font-weight: 700; }
.verdict { font-size: 14px; color: var(--ink); margin-top: 4px; max-width: 34em; white-space: normal; }
.verdict ul { margin: 4px 0 0; padding-left: 18px; }

.log-list { list-style: none; margin: 0; padding: 0; background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius); }
.log-list li { padding: 10px 14px; border-top: 1px solid var(--rule-soft); display: grid; grid-template-columns: 150px 1fr; gap: 4px 16px; }
.log-list li:first-child { border-top: 0; }
.log-list time { color: var(--ink-2); font-size: 13px; }

/* ── 對話框 ── */
dialog { border: 0; border-radius: 10px; padding: 0; width: min(520px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); color: var(--ink); }
dialog::backdrop { background: rgba(22, 48, 43, .5); }
.form { display: grid; gap: 12px; padding: 20px; }
.form h2 { font-size: 20px; }
.form .row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; align-items: start; }
.form .row2:not(:has(> :not([hidden]))) { display: none; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 4px; }
.form-actions .spacer { flex: 1; }
.seg-choice { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; border: 1px solid var(--rule); border-radius: var(--radius); overflow: hidden; }
.seg-choice label { display: block; text-align: center; padding: 8px 6px; cursor: pointer; color: var(--ink); font-size: 15px; border-left: 1px solid var(--rule); }
.seg-choice label:first-child { border-left: 0; }
.seg-choice input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.seg-choice label:has(input:checked) { background: var(--ink); color: #fff; }
.seg-choice label:has(input:focus-visible) { outline: 2px solid var(--green); outline-offset: -2px; }
fieldset { border: 0; padding: 0; margin: 0; display: grid; gap: 4px; }
legend { font-size: 14px; color: var(--ink-2); padding: 0; margin-bottom: 4px; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 99px; font-size: 15px; opacity: 0; pointer-events: none; transition: opacity .2s; max-width: calc(100vw - 32px); z-index: 20; }
#toast.show { opacity: 1; }
#toast.bad { background: var(--seal); }

/* ── 手機 ── */
@media (max-width: 720px) {
  .top-inner { flex-wrap: wrap; gap: 0 12px; padding-top: 8px; }
  .brand { flex: 1; }
  .tabs { order: 3; flex-basis: 100%; margin: 0 -8px; }
  .tabs button { padding: 10px 10px; }
  .standing { grid-template-columns: 1fr; }
  .standing-actions { justify-content: flex-start; }
  .item { grid-template-columns: 1fr auto; }
  .item-bar { grid-column: 1 / -1; grid-row: 2; }
  .filters { grid-template-columns: 1fr 1fr; }
  .filters input { grid-column: 1 / -1; }
  .form .row2 { grid-template-columns: 1fr; }
  .log-list li { grid-template-columns: 1fr; }
  /* 支出表改成一筆一張 */
  table.cards thead { display: none; }
  table.cards, table.cards tbody, table.cards tr, table.cards td { display: block; width: 100%; }
  table.cards tr { padding: 10px 12px; border-bottom: 1px solid var(--rule); }
  table.cards td { padding: 2px 0; border: 0; text-align: left; }
  table.cards td.num { font-size: 18px; }
  table.cards td.actions { text-align: left; padding-top: 8px; }
  table.cards td.actions .btn { margin: 0 4px 0 0; }
  table.cards tfoot tr { background: #fafbf9; }
}
@media (prefers-reduced-motion: reduce) { #toast { transition: none; } }
