:root {
  --brand: #c8102e;
  --brand-dark: #8b0a20;
  --bg: #fafafa;
  --card: #ffffff;
  --border: #e5e7eb;
  --text: #1f2937;
  --muted: #6b7280;
  --ok: #16a34a;
  --warn: #f59e0b;
  --empty: #9ca3af;
  --accent: #f97316;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}
.wrap { max-width: 960px; margin: 0 auto; padding: 24px 16px 64px; }
header {
  background: linear-gradient(135deg, var(--brand) 0%, var(--brand-dark) 100%);
  color: white;
  padding: 24px;
  border-radius: 12px;
  margin-bottom: 16px;
  box-shadow: 0 4px 12px rgba(200, 16, 46, .15);
}
header h1 { margin: 0 0 6px; font-size: 22px; }
header .sub { font-size: 14px; opacity: .92; }
header .sub a { color: #fff; text-decoration: underline; }

.tabs {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
}
.tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 18px;
  border: 1px solid transparent;
  border-bottom: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  border-radius: 8px 8px 0 0;
  margin-bottom: -1px;
}
.tab:hover { color: var(--text); }
.tab.active { background: var(--card); border-color: var(--border); color: var(--brand); }

.tab-action {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px 10px;
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--muted);
  border-radius: 8px;
  transition: background .15s, color .15s;
}
.tab-action:hover { color: var(--brand); background: var(--card); }
.tab-action.active { color: var(--brand); background: var(--card); border: 1px solid var(--border); border-bottom-color: var(--card); border-radius: 8px 8px 0 0; margin-bottom: -1px; padding: 9px 10px; }
.tab-action i, .tab-action svg { width: 20px; height: 20px; }
.pane { display: none; }
.pane.active { display: block; }

.controls {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 16px;
  display: grid;
  grid-template-columns: 1fr 2fr auto;
  gap: 12px;
  align-items: end;
}
@media (max-width: 640px) { .controls { grid-template-columns: 1fr; } }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.field select, .field input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  background: white;
  color: var(--text);
}
.field select:focus, .field input:focus { outline: 2px solid var(--brand); outline-offset: -2px; }

button.primary {
  background: var(--brand);
  color: white;
  border: none;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
button.primary:hover { background: var(--brand-dark); }
button.primary:disabled { background: #d1d5db; cursor: wait; }
button.ghost {
  background: #f3f4f6;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  padding: 8px 14px;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
button.ghost:hover { background: #e5e7eb; }
button.icon-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--muted);
  padding: 6px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
button.icon-btn:hover { background: #f3f4f6; color: var(--text); }

/* Lucide icon sizing inside buttons / text */
button i[data-lucide], button svg.lucide { width: 16px; height: 16px; }
.icon-btn i[data-lucide], .icon-btn svg.lucide { width: 18px; height: 18px; }
.tab i[data-lucide], .tab svg.lucide { width: 16px; height: 16px; }

.status {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 16px;
  font-size: 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.status .meta { color: var(--muted); }
.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.badge.ok { background: #dcfce7; color: var(--ok); }
.badge.warn { background: #fef3c7; color: #92400e; }
.badge.err { background: #fee2e2; color: #991b1b; }
.badge.load { background: #e0e7ff; color: #3730a3; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}
.item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: transform .15s, box-shadow .15s;
}
.item:hover { transform: translateY(-2px); box-shadow: 0 6px 16px rgba(0,0,0,.06); }
.item .name { font-weight: 600; font-size: 15px; }
.item .sub { font-size: 12px; color: var(--muted); }
.item .row { display: flex; justify-content: space-between; align-items: baseline; margin-top: 6px; }
.item .price { color: var(--brand); font-weight: 700; font-size: 18px; }
.item .qty {
  background: var(--ok);
  color: white;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
}
.item .qty.low { background: var(--warn); }
.item .qty.zero { background: var(--empty); }

.empty {
  background: var(--card);
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 36px;
  text-align: center;
  color: var(--muted);
}

.product-list { display: flex; flex-direction: column; gap: 12px; }
.product-card { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 16px; }
.product-card .head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.product-card .name { font-size: 16px; font-weight: 600; }
.product-card .code { font-size: 12px; color: var(--muted); margin-top: 2px; }
.product-card .totals { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.stat { display: flex; flex-direction: column; align-items: flex-end; }
.stat .label { font-size: 11px; color: var(--muted); }
.stat .value { font-size: 18px; font-weight: 700; color: var(--brand); }
.stat .value.qty { color: var(--ok); }

.station-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: #f3f4f6;
  color: var(--text);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  border: 1px solid var(--border);
}
.chip.err {
  background: #fef2f2;
  border-color: #fecaca;
  color: #991b1b;
}
.chip .chip-qty {
  background: var(--ok);
  color: white;
  padding: 0 6px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.chip .chip-qty.low { background: var(--warn); }

.summary-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}
.summary-row .box {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  text-align: center;
}
.summary-row .box .lbl { font-size: 12px; color: var(--muted); }
.summary-row .box .num { font-size: 22px; font-weight: 700; color: var(--brand); margin-top: 4px; }

/* ---- Settings page ---- */
.settings-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px 22px;
}
.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}
.settings-header h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.settings-header h2 i[data-lucide],
.settings-header h2 svg.lucide { width: 18px; height: 18px; }

.settings-card .body { font-size: 13px; }
.settings-card .body .row {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: 8px;
  flex-wrap: wrap;
}
.settings-card input[type=text],
.settings-card input[type=password] {
  flex: 1;
  min-width: 200px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
}
.settings-card .field-label { font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.settings-card .hint { margin-top: 8px; font-size: 12px; color: var(--muted); }

/* settings 內層頁籤 */
.subtabs {
  display: flex;
  gap: 4px;
  margin-bottom: 14px;
  border-bottom: 1px solid var(--border);
}
.subtab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid transparent;
  border-bottom: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  border-radius: 8px 8px 0 0;
  margin-bottom: -1px;
}
.subtab:hover { color: var(--text); }
.subtab.active { color: var(--brand); border-color: var(--border); background: var(--card); }
.subtab i[data-lucide],
.subtab svg.lucide { width: 15px; height: 15px; }
.subpane[hidden] { display: none; }

/* 更新進度條 */
.progress-track {
  height: 10px;
  background: #f3f4f6;
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 6px;
}
#progressFill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--brand) 0%, var(--accent) 100%);
  border-radius: 999px;
  transition: width .2s ease-out;
}
.progress-meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--muted);
}

/* 更新站點清單 */
.station-list {
  max-height: 440px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-top: 12px;
  background: #fff;
}
.station-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  transition: background .15s, color .15s;
}
.station-row:last-child { border-bottom: none; }
.station-row.state-running { background: #eef2ff; }
.station-row.state-success { color: var(--text); }
.station-row.state-failed { color: var(--text); }

.station-row .station-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.station-status {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  flex-shrink: 0;
  max-width: 60%;
}
.station-status.idle    { color: var(--muted); }
.station-status.running { color: #3730a3; font-weight: 600; }
.station-status.success { color: var(--ok); }
.station-status.failed  {
  color: #b91c1c;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.station-status i[data-lucide],
.station-status svg.lucide { width: 14px; height: 14px; flex-shrink: 0; }
.station-status.running svg.lucide { animation: yt-spin 1s linear infinite; }
@keyframes yt-spin { to { transform: rotate(360deg); } }

/* 捷運線代表圖示 */
.line-icon {
  width: 16px;
  height: 16px;
  margin-right: 4px;
  vertical-align: -3px;
  flex-shrink: 0;
  border-radius: 3px;
}
.line-icon-store { vertical-align: -3px; color: var(--muted); }
.line-icon-lg { width: 22px; height: 22px; vertical-align: -5px; margin-right: 6px; }
.chip .line-icon { width: 14px; height: 14px; margin-right: 3px; vertical-align: -2px; }
.station-row .line-icon { width: 18px; height: 18px; margin-right: 6px; vertical-align: -4px; }

footer { margin-top: 24px; text-align: center; font-size: 12px; color: var(--muted); }
.err-detail {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #991b1b;
  padding: 12px;
  border-radius: 8px;
  font-size: 13px;
  margin-top: 8px;
  word-break: break-all;
}
