:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-2: #f9fafb;
  --line: #e5e7eb;
  --line-strong: #d1d5db;
  --text: #1f2430;
  --muted: #6b7280;
  --accent: #4f6bed;
  --accent-text: #ffffff;
  --danger: #d64545;
  --late: #e5484d;
  --today: #e5484d;
  --weekend: rgba(100, 116, 139, 0.07);
  --hover: rgba(79, 107, 237, 0.05);
  --selected: rgba(79, 107, 237, 0.12);
  --cat-bg: #eef0f4;
  --shadow: 0 8px 28px rgba(15, 23, 42, 0.18);
  --row-h: 40px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121418;
    --panel: #1a1d23;
    --panel-2: #20242b;
    --line: #2b3039;
    --line-strong: #3a404b;
    --text: #e5e7ec;
    --muted: #9aa1ad;
    --accent: #6d86ff;
    --weekend: rgba(148, 163, 184, 0.06);
    --hover: rgba(109, 134, 255, 0.07);
    --selected: rgba(109, 134, 255, 0.16);
    --cat-bg: #232831;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  font: 13px/1.4 "Segoe UI", "Microsoft JhengHei", "PingFang TC", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
}

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

button, select, input[type="search"], input[type="text"], input[type="date"], textarea {
  border: 1px solid var(--line-strong);
  background: var(--panel);
  border-radius: 6px;
  padding: 4px 10px;
  min-height: 28px;
}

button { cursor: pointer; }
button:hover { border-color: var(--accent); }
button:disabled { opacity: 0.45; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.btn-primary, .btn-accent { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover, .btn-accent:hover { filter: brightness(1.08); }
.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.btn-ghost { background: transparent; color: var(--muted); }

/* ---------- toolbar ---------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px 16px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.tb-group { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.app-title { font-size: 16px; margin: 0 8px 0 0; }
.chk { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); user-select: none; }
.file-status { color: var(--muted); margin-left: 4px; }
.file-status.dirty { color: #c27c0e; }
.file-status.ok { color: #1f9d6b; }
#f-search { width: 150px; }

.seg { display: inline-flex; }
.seg button { border-radius: 0; margin-left: -1px; }
.seg button:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.seg button.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); position: relative; }

/* ---------- gantt ---------- */
.gantt {
  flex: 1;
  overflow: auto;
  position: relative;
  background: var(--panel);
  outline: none;
}
.g-inner { position: relative; min-height: 100%; }

.g-head {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  background: var(--panel-2);
  border-bottom: 1px solid var(--line-strong);
}
.g-head-left {
  position: sticky;
  left: 0;
  z-index: 6;
  flex: none;
  display: grid;
  align-items: end;
  padding: 0 0 6px;
  background: var(--panel-2);
  border-right: 1px solid var(--line-strong);
  color: var(--muted);
  font-size: 12px;
}
.g-head-right { position: relative; flex: none; }
.h-cell {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  white-space: nowrap;
  border-left: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
}
.h-top { top: 0; height: 24px; justify-content: flex-start; padding-left: 6px; color: var(--text); font-weight: 600; }
.h-bot { top: 24px; height: 30px; flex-direction: column; line-height: 1.1; }
.h-bot small { font-size: 10px; opacity: 0.8; }
.h-bot.weekend { color: color-mix(in srgb, var(--muted) 70%, transparent); }
.h-bot.holiday { color: var(--late); }
.h-bot.today { color: var(--today); font-weight: 700; }

.cols { display: grid; grid-template-columns: 1fr 40px 58px 36px 84px 96px 116px 52px; align-items: center; height: 100%; }
.cols > * { padding: 0 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.g-row.cat .cols > .name { grid-column: 1 / 5; }
.g-row.cat .cols > .cat-prog { grid-column: 5 / 6; }
.g-row.cat .cols > .cat-hrs { grid-column: 8 / 9; }
.hrs-cell, .cat-hrs { text-align: right; font-size: 12px; font-variant-numeric: tabular-nums; }
.hrs-miss { color: var(--muted); font-style: normal; }

/* 完成進度 */
.prog { display: flex; align-items: center; gap: 5px; }
.prog-bar {
  flex: 1;
  height: 6px;
  min-width: 24px;
  border-radius: 3px;
  background: color-mix(in srgb, var(--muted) 20%, transparent);
  overflow: hidden;
}
.prog-bar i { display: block; height: 100%; border-radius: 3px; background: var(--c, var(--accent)); }
.prog.full .prog-bar i { background: #10b981; }
.prog-txt { flex: none; width: 30px; text-align: right; font-size: 11px; font-variant-numeric: tabular-nums; color: var(--muted); }
.prog.full .prog-txt { color: #059669; }
.g-row.cat .prog-bar { height: 8px; }
.g-row.cat .prog-txt { color: var(--text); font-weight: 600; }
.prog-input { display: flex; align-items: center; gap: 8px; }
.prog-input input[type="range"] { flex: 1; accent-color: var(--accent); }
.prog-input input[type="number"] { width: 64px; min-height: 28px; padding: 2px 6px; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--panel); color: var(--text); }
.hint-inline { margin-left: 6px; color: var(--muted); font-weight: 400; }

/* 左側列表寬度調整 */
.g-left, .g-head-left { width: var(--left-w, 480px); }
.left-resizer {
  position: absolute;
  top: 0;
  right: -4px;
  bottom: 0;
  width: 8px;
  cursor: col-resize;
  z-index: 7;
}
.left-resizer::after {
  content: "";
  position: absolute;
  top: 10px; bottom: 10px; left: 3px;
  width: 2px;
  border-radius: 1px;
  background: var(--line-strong);
}
.left-resizer:hover::after, .resizing .left-resizer::after { background: var(--accent); }
body.resizing, body.resizing * { cursor: col-resize !important; user-select: none; }

/* 留言註記 */
.cmt-cell { padding: 0 !important; text-align: center; }
.cmt-btn {
  min-height: 0; height: 24px;
  padding: 0 4px;
  border: 1px solid transparent;
  background: transparent;
  font-size: 12px;
  line-height: 22px;
  opacity: 0.3;
  filter: grayscale(1);
}
.g-row:hover .cmt-btn { opacity: 0.65; }
.cmt-btn:hover { opacity: 1 !important; filter: none; border-color: var(--line-strong); }
.cmt-btn.has { opacity: 1; filter: none; }
.cmt-btn b { margin-left: 1px; color: var(--accent); font-size: 11px; }
.m-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 300px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.m-list li { padding: 8px 12px; border-bottom: 1px solid var(--line); }
.m-list li:last-child { border-bottom: 0; }
.m-meta { display: flex; align-items: center; gap: 8px; font-size: 12px; }
.m-meta time { color: var(--muted); }
.m-del { margin-left: auto; min-height: 0; height: 20px; padding: 0 6px; font-size: 11px; color: var(--muted); background: transparent; border-color: transparent; }
.m-del:hover { color: var(--danger); border-color: var(--danger); }
.m-text { margin-top: 2px; white-space: pre-wrap; word-break: break-word; }
.m-empty { color: var(--muted); text-align: center; }

.g-grid { position: absolute; z-index: 0; pointer-events: none; }
.g-grid .wk { position: absolute; top: 0; bottom: 0; background: var(--weekend); }
.g-grid .wk.hol { background: color-mix(in srgb, var(--late) 7%, var(--weekend)); }
.g-grid .vl { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--line); }
.g-grid .vl.strong { background: var(--line-strong); }
.g-grid .vl.half { background: none; border-left: 1px dashed color-mix(in srgb, var(--line) 70%, transparent); }
.g-grid .td { position: absolute; top: 0; bottom: 0; background: color-mix(in srgb, var(--today) 7%, transparent); }
.g-grid .tl { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--today); opacity: 0.75; }

.g-row { position: relative; z-index: 1; display: flex; height: var(--row-h); border-bottom: 1px solid var(--line); }
.g-row:hover { background: var(--hover); }
.g-row.selected { background: var(--selected); }
.g-left {
  position: sticky;
  left: 0;
  z-index: 3;
  flex: none;
  background: var(--panel);
  border-right: 1px solid var(--line-strong);
  cursor: default;
  user-select: none;
}
.g-row:hover .g-left { background: color-mix(in srgb, var(--panel) 92%, var(--accent)); }
.g-row.selected .g-left { background: color-mix(in srgb, var(--panel) 84%, var(--accent)); }
.g-tl { position: relative; flex: none; cursor: crosshair; }
.g-fill { position: relative; z-index: 1; display: flex; }
.g-fill .g-left { height: 100%; }

.g-row.cat { background: color-mix(in srgb, var(--cat-bg) 70%, transparent); }
.g-row.cat .g-left { background: var(--cat-bg); font-weight: 600; }
.g-row.cat .g-tl { cursor: default; }

.name { display: flex; align-items: center; gap: 4px; min-width: 0; }
.name .title { overflow: hidden; text-overflow: ellipsis; }
.name .title.done { color: var(--muted); text-decoration: line-through; }
.caret {
  flex: none;
  width: 18px; height: 18px; min-height: 0;
  padding: 0; border: 0; background: transparent;
  color: var(--muted); font-size: 10px; line-height: 18px; text-align: center;
}
.caret.empty { visibility: hidden; }
.swatch { flex: none; width: 10px; height: 10px; border-radius: 3px; background: var(--c); }
.count { color: var(--muted); font-weight: 400; font-size: 12px; }
.late-dot { flex: none; width: 7px; height: 7px; border-radius: 50%; background: var(--late); }

.row-acts { display: none; margin-left: auto; gap: 2px; flex: none; }
.g-row:hover .row-acts { display: inline-flex; }
.row-acts button { min-height: 0; height: 22px; padding: 0 6px; font-size: 12px; background: var(--panel); }

.chip { display: inline-block; padding: 1px 6px; border-radius: 10px; font-size: 11px; line-height: 16px; }
.st-todo { background: color-mix(in srgb, #94a3b8 22%, transparent); }
.st-doing { background: color-mix(in srgb, #3b82f6 22%, transparent); color: #2563eb; }
.st-paused { background: color-mix(in srgb, #f59e0b 24%, transparent); color: #b45309; }
.st-done { background: color-mix(in srgb, #10b981 22%, transparent); color: #059669; }
.st-delivered { background: color-mix(in srgb, #8b5cf6 22%, transparent); color: #7c3aed; }
@media (prefers-color-scheme: dark) {
  .st-doing { color: #93b4ff; }
  .st-paused { color: #fbbf24; }
  .st-done { color: #4ade9a; }
  .st-delivered { color: #c4b5fd; }
}
.chip.tbd { padding: 0 5px; border: 1px dashed var(--muted); color: var(--muted); }
.tbd-tag {
  flex: none;
  padding: 0 5px;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 400;
}
.pr-high { color: var(--late); font-weight: 700; }
.pr-medium { color: #c27c0e; }
.pr-low { color: var(--muted); }
.muted { color: var(--muted); }
.date-txt { font-size: 12px; font-variant-numeric: tabular-nums; }
.date-txt.late { color: var(--late); }
.seg-n { margin-left: 4px; padding: 0 4px; border-radius: 4px; background: var(--cat-bg); color: var(--muted); font-size: 11px; }

/* bars */
.bar {
  position: absolute;
  z-index: 2;
  height: 12px;
  border-radius: 4px;
  cursor: grab;
  touch-action: none;
}
.bar.plan {
  top: 7px;
  /* 依完成進度 --p 由左填入較深色 */
  background:
    linear-gradient(to right, color-mix(in srgb, var(--c) 60%, transparent) var(--p, 0%), transparent var(--p, 0%)),
    color-mix(in srgb, var(--c) 22%, transparent);
  border: 1px solid color-mix(in srgb, var(--c) 70%, transparent);
}
.bar.actual { top: 21px; background: var(--c); }
.bar.actual.open {
  background: repeating-linear-gradient(135deg, var(--c) 0 6px, color-mix(in srgb, var(--c) 65%, transparent) 6px 12px);
  border-radius: 4px 0 0 4px;
}
/* 實際執行段與段之間的暫停：虛線連接 */
.seg-gap { position: absolute; z-index: 1; top: 21px; height: 12px; }
.seg-gap::before {
  content: ""; position: absolute; left: 0; right: 0; top: 5px;
  border-top: 2px dotted color-mix(in srgb, var(--c) 65%, transparent);
}
.bar.actual.late { box-shadow: 0 0 0 2px color-mix(in srgb, var(--late) 70%, transparent); }
.bar.summary { cursor: default; border-radius: 1px; }
.bar.summary::before, .bar.summary::after {
  content: ""; position: absolute; bottom: -4px; width: 0; height: 0;
  border-left: 4px solid transparent; border-right: 4px solid transparent;
  border-top: 4px solid var(--c);
}
.bar.summary::before { left: -1px; }
.bar.summary::after { right: -1px; }
.bar.deadline {
  top: 6px;
  width: 14px;
  height: 14px;
  border-radius: 0;
  background: var(--c);
  clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
}
.bar.preview { opacity: 0.7; pointer-events: none; }
.bar.dragging { cursor: grabbing; opacity: 0.85; }
.h-l, .h-r { position: absolute; top: -2px; bottom: -2px; width: 8px; cursor: ew-resize; }
.h-l { left: -4px; }
.h-r { right: -4px; }
.bar:hover { filter: brightness(1.05); }

.empty-state { padding: 40px; color: var(--muted); text-align: center; }

/* ---------- legend ---------- */
.legend {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 16px;
  padding: 6px 16px;
  background: var(--panel);
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
}
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend .hint { margin-left: auto; }
.lg { display: inline-block; width: 24px; height: 10px; border-radius: 3px; --c: var(--accent); }
.lg-plan { background: color-mix(in srgb, var(--c) 28%, transparent); border: 1px solid color-mix(in srgb, var(--c) 70%, transparent); }
.lg-actual { background: var(--c); }
.lg-open { background: repeating-linear-gradient(135deg, var(--c) 0 5px, color-mix(in srgb, var(--c) 65%, transparent) 5px 10px); }
.lg-deadline {
  display: inline-block; width: 12px; height: 12px;
  background: var(--accent);
  clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
}
.lg-gap { display: inline-block; width: 24px; border-top: 2px dotted var(--accent); }
.lg-late { background: var(--c); box-shadow: 0 0 0 2px color-mix(in srgb, var(--late) 70%, transparent); }

/* ---------- tooltip ---------- */
.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  padding: 4px 8px; border-radius: 6px;
  background: #1f2430; color: #fff; font-size: 12px; white-space: nowrap;
  box-shadow: var(--shadow);
}

/* ---------- dialogs ---------- */
.dlg {
  width: min(560px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
}
#dlg-task, .dlg-lg { width: min(660px, calc(100vw - 32px)); }
.dlg-sm { width: min(380px, calc(100vw - 32px)); }
.dlg::backdrop { background: rgba(15, 23, 42, 0.35); }
.dlg form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; padding: 18px 20px; }
.dlg h2 { grid-column: 1 / -1; margin: 0 0 4px; font-size: 16px; }
.fld { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 12px; }
.fld input, .fld select, .fld textarea { color: var(--text); font-size: 13px; }
.fld-full { grid-column: 1 / -1; }
.dates { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; margin: 0; padding: 8px 12px 12px; border: 1px solid var(--line); border-radius: 8px; }
.dates legend { padding: 0 4px; color: var(--muted); font-size: 12px; }
.quick { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 6px; }
.segs { grid-column: 1 / -1; display: flex; flex-direction: column; gap: 6px; }
.seg-row { display: grid; grid-template-columns: 18px 1fr auto 1fr 70px auto; align-items: center; gap: 6px; }
.seg-row input { min-width: 0; font-size: 13px; }
.dt { display: flex; gap: 4px; min-width: 0; }
.dt input { flex: 1; min-width: 0; }
.dt .half { flex: none; padding: 4px 4px; font-size: 13px; color: var(--text); }
.seg-no { color: var(--muted); font-size: 12px; text-align: center; }
.seg-del { min-height: 0; padding: 2px 8px; color: var(--muted); }
.seg-del:hover { color: var(--danger); border-color: var(--danger); }
.seg-empty, .seg-hint { grid-column: 1 / -1; color: var(--muted); font-size: 12px; }
.note { margin: 0; padding: 6px 10px; border-radius: 6px; background: var(--cat-bg); color: var(--muted); font-size: 12px; }
.error { margin: 0; color: var(--danger); }
.dlg-actions { display: flex; gap: 8px; margin-top: 4px; }
.spacer { flex: 1; }
.swatches { display: flex; flex-wrap: wrap; gap: 6px; margin: 2px 0 4px; }
.swatches button { width: 24px; height: 24px; min-height: 0; padding: 0; border-radius: 50%; background: var(--c); border: 2px solid transparent; }
.swatches button.on { border-color: var(--text); }
#c-color { width: 60px; height: 28px; padding: 0 2px; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--panel); }

@media (max-width: 720px) {
  .legend .hint { display: none; }
  .dlg form, .dates { grid-template-columns: 1fr; }
}

/* 每段工時 */
.hrs-in { display: flex; align-items: center; gap: 3px; color: var(--muted); font-size: 12px; }
.hrs-in input { width: 100%; min-width: 0; min-height: 28px; padding: 4px 6px; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--panel); text-align: right; }

/* ---------- 工時統計 ---------- */
.stats-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.stats-custom { display: inline-flex; align-items: center; gap: 4px; }
.stats-custom[hidden] { display: none; }
.stats-body { max-height: 55vh; overflow: auto; border: 1px solid var(--line); border-radius: 8px; }
.stats-body > .m-empty { margin: 0; padding: 24px; }
.stats { width: 100%; border-collapse: collapse; font-size: 13px; }
.stats th, .stats td { padding: 6px 10px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.stats thead th { position: sticky; top: 0; background: var(--panel-2); color: var(--muted); font-weight: 600; font-size: 12px; }
.stats .num { text-align: right; font-variant-numeric: tabular-nums; }
.stats .s-cat td { background: var(--cat-bg); font-weight: 600; }
.stats .s-cat .swatch { display: inline-block; margin-right: 6px; vertical-align: -1px; }
.stats .s-task { max-width: 360px; overflow: hidden; text-overflow: ellipsis; padding-left: 26px; }
.stats tfoot td { font-weight: 700; border-bottom: 0; }
.s-bar { display: inline-block; width: 90px; height: 6px; margin-right: 6px; border-radius: 3px; background: var(--line); vertical-align: middle; overflow: hidden; }
.s-bar i { display: block; height: 100%; }
.s-pct { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.stats-miss { margin: 0; padding: 8px 10px; color: var(--muted); font-size: 12px; }

/* ---------- 假日設定 ---------- */
#dlg-hol textarea { font: 12px/1.6 ui-monospace, Consolas, "Microsoft JhengHei", monospace; resize: vertical; }
#dlg-hol code { padding: 0 4px; border-radius: 4px; background: var(--panel); }
