:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #16a34a;
  --brand-2: #15803d;
  --danger: #ef4444;
  --warn: #f59e0b;
  --ok: #10b981;
  --radius: 14px;
  --shadow: 0 8px 24px rgba(15, 23, 42, .08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC",
    "Microsoft JhengHei", "Noto Sans TC", sans-serif;
  -webkit-font-smoothing: antialiased;
}

body {
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
  min-height: 100vh;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px calc(14px);
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: #fff;
}
.topbar h1 { margin: 0; font-size: 20px; font-weight: 700; }
.icon-btn {
  border: 0;
  background: rgba(255, 255, 255, .18);
  color: #fff;
  border-radius: 10px;
  padding: 6px 10px;
  font-size: 16px;
  cursor: pointer;
}

.view { padding: 18px; max-width: 760px; margin: 0 auto; }

h2 { font-size: 15px; color: var(--muted); margin: 22px 4px 10px; font-weight: 600; }

/* tab bar */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  justify-content: space-around;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding: 8px 0 calc(8px + env(safe-area-inset-bottom));
  z-index: 30;
}
.tabbar a {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 11px;
  color: var(--muted);
  text-decoration: none;
  padding: 4px 18px;
  border-radius: 10px;
}
.tabbar a.active { color: var(--brand-2); font-weight: 700; }

/* item cards */
.item-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.item-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.item-card .emoji { font-size: 26px; }
.item-card .meta { flex: 1; min-width: 0; }
.item-card .meta strong { display: block; font-size: 15px; }
.item-card .meta small { color: var(--muted); }

.badge {
  font-size: 12px;
  font-weight: 700;
  padding: 5px 10px;
  border-radius: 999px;
  white-space: nowrap;
}
.badge.ok { background: #dcfce7; color: #15803d; }
.badge.warn { background: #fef3c7; color: #b45309; }
.badge.danger { background: #fee2e2; color: #b91c1c; }
.badge.neutral { background: #e2e8f0; color: #475569; }

/* buttons */
button, .btn {
  font: inherit;
  cursor: pointer;
  border-radius: 12px;
  border: 1px solid transparent;
  padding: 11px 16px;
  background: var(--brand-2);
  color: #fff;
  font-weight: 600;
}
button.secondary, .btn.secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--line);
}
a.btn { text-decoration: none; display: inline-flex; align-items: center; justify-content: center; }
button.danger { background: var(--danger); }
button:disabled { opacity: .5; cursor: not-allowed; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }

/* forms */
form { display: grid; gap: 14px; }
label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); font-weight: 600; }
input, select, textarea {
  font: inherit;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  width: 100%;
}
fieldset {
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  padding: 14px;
  display: grid;
  gap: 12px;
}
legend { padding: 0 8px; color: var(--muted); font-weight: 700; font-size: 13px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  line-height: 1.2;
  cursor: pointer;
}
.chip.selected { background: var(--brand-2); color: #fff; border-color: var(--brand-2); }
/* Never let a chip inherit the white button text on a white background. */
.chip:not(.selected) { color: var(--text); }
.chip-ico { font-size: 14px; }
.chip b { font-weight: 800; }

/* lead-day chips: readable numbers, clear selected state */
.lead-chip { font-size: 14px; padding: 9px 14px; }
.lead-chip.selected b { color: #fff; }
.lead-custom { border-style: dashed; color: var(--brand-2); font-weight: 700; }
.lead-custom:not(.selected) { color: var(--brand-2); }

/* grey the select when it is showing the "請選擇…" prompt */
select.is-empty { color: var(--muted); }
/* placeholders + required hints */
input::placeholder { color: #94a3b8; }

/* filters */
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.filters button { padding: 8px 14px; font-size: 13px; background: var(--surface); color: var(--text); border: 1px solid var(--line); }
.filters button.active { background: var(--brand-2); color: #fff; border-color: var(--brand-2); }

/* search box */
.search-row { position: relative; margin-bottom: 10px; }
.search-row #search {
  width: 100%;
  padding: 12px 40px 12px 14px;
  font-size: 15px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
}
.search-row #search:focus { outline: none; border-color: var(--brand-2); box-shadow: 0 0 0 3px rgba(22,163,74,.15); }
.search-clear {
  position: absolute;
  right: 8px; top: 50%;
  transform: translateY(-50%);
  width: 28px; height: 28px;
  padding: 0;
  border-radius: 50%;
  background: #e2e8f0;
  color: #475569;
  font-size: 13px;
  line-height: 1;
  display: grid; place-items: center;
}

/* camera */
.camera-wrap {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  background: #0b1220;
  aspect-ratio: 3 / 4;
  max-height: 62vh;
}
.camera-wrap video { width: 100%; height: 100%; object-fit: cover; }
.camera-wrap.flash::after {
  content: '';
  position: absolute; inset: 0;
  background: #fff;
  animation: flashfade .18s ease-out;
  pointer-events: none;
}
@keyframes flashfade { from { opacity: .85; } to { opacity: 0; } }

.scan-overlay {
  position: absolute; inset: 12% 8%;
  border: 2px solid rgba(255,255,255,.9);
  border-radius: 18px;
  display: flex; align-items: flex-start; justify-content: center;
  color: #fff; font-size: 13px; padding-top: 10px;
  text-shadow: 0 1px 3px rgba(0,0,0,.6);
  pointer-events: none;
  /* Dim everything outside the guide box so the capture area is obvious. */
  box-shadow: 0 0 0 100vmax rgba(0,0,0,.38);
}

.scan-preview {
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  background: #0b1220;
}
.scan-preview img {
  display: block;
  width: 100%;
  max-height: 200px;
  object-fit: contain;
  background: #0b1220;
}
.scan-preview figcaption {
  padding: 7px 12px;
  font-size: 12px;
  color: var(--muted);
  background: var(--surface);
  text-align: center;
}

/* big shutter button overlaid on the camera so it's always visible */
.shutter {
  position: absolute;
  left: 50%; bottom: 18px;
  transform: translateX(-50%);
  width: 74px; height: 74px;
  padding: 0;
  border-radius: 50%;
  background: rgba(255,255,255,.25);
  border: 4px solid #fff;
  box-shadow: 0 6px 18px rgba(0,0,0,.35);
  display: grid; place-items: center;
  cursor: pointer;
}
.shutter::after {
  content: '';
  width: 56px; height: 56px;
  border-radius: 50%;
  background: #fff;
}
.shutter:active { transform: translateX(-50%) scale(.93); }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.scan-status {
  margin: 12px 0;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  font-size: 14px;
  color: var(--muted);
  text-align: center;
}

/* toast */
.toast {
  position: fixed;
  left: 50%; bottom: calc(88px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: #0f172a;
  color: #fff;
  padding: 12px 18px;
  border-radius: 12px;
  font-size: 14px;
  z-index: 50;
  max-width: 90vw;
}
.toast.error { background: var(--danger); }

/* misc */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.muted { color: var(--muted); }

/* ---- friendly key/value view (replaces raw JSON) ---- */
.kv-section { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 12px; }
.kv-section-title { font-size: 13px; font-weight: 700; color: var(--muted); margin-bottom: 8px; }
.kv-list { display: grid; grid-template-columns: minmax(96px, 34%) 1fr; gap: 8px 14px; margin: 0; }
.kv-list dt { color: var(--muted); font-weight: 600; font-size: 14px; }
.kv-list dd { margin: 0; font-size: 14px; word-break: break-word; }
.kv-sub { display: inline-block; margin-right: 12px; }
.kv-sub b { color: var(--muted); font-weight: 600; margin-right: 4px; }
.kv-empty { color: var(--muted); font-size: 14px; padding: 4px 0; }

/* ---- JSON <-> interface editor (advanced, collapsed) ---- */
.json-editor { margin-top: 4px; }
.json-advanced {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}
.json-advanced summary {
  cursor: pointer;
  padding: 12px 14px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  list-style: none;
}
.json-advanced summary::-webkit-details-marker { display: none; }
.json-advanced summary::before { content: '⚙️ '; }
.json-advanced[open] summary { border-bottom: 1px solid var(--line); }
.json-advanced-body { padding: 14px; display: grid; gap: 10px; }
.json-hint { font-size: 12px; margin: 0; }
.json-input {
  width: 100%;
  min-height: 160px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #0f172a;
  color: #e2e8f0;
  resize: vertical;
}
.json-error {
  color: var(--danger);
  font-size: 13px;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 8px;
  padding: 8px 10px;
}
.json-preview-label { font-size: 12px; }
.json-preview {
  border: 1px dashed var(--line);
  border-radius: 10px;
  padding: 10px 12px;
}

/* ---- dashboard multi-select ---- */
.item-card.selectable { cursor: pointer; }
.item-card.selectable .sel-mark {
  flex: none;
  width: 22px; height: 22px;
  border-radius: 50%;
  border: 2px solid var(--line);
  background: var(--surface);
  display: grid; place-items: center;
}
.item-card.selected {
  border-color: var(--brand-2);
  box-shadow: 0 0 0 2px rgba(22, 163, 74, .25);
}
.item-card.selected .sel-mark {
  background: var(--brand-2);
  border-color: var(--brand-2);
}
.item-card.selected .sel-mark::after {
  content: '✓';
  color: #fff;
  font-size: 13px;
  font-weight: 900;
}
.bulk-bar {
  position: sticky;
  bottom: calc(72px + env(safe-area-inset-bottom));
  margin-top: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  box-shadow: var(--shadow);
  z-index: 25;
}
.bulk-count { font-weight: 700; font-size: 14px; }
.bulk-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.bulk-actions button { padding: 8px 14px; font-size: 13px; }
#btn-select.active { background: var(--brand-2); color: #fff; border-color: var(--brand-2); }
.row { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.stack { display: grid; gap: 14px; }
.center { text-align: center; }
.auth-wrap { max-width: 420px; margin: 6vh auto 0; }
.tabs { display: flex; gap: 8px; margin-bottom: 14px; }
.tabs button { flex: 1; background: var(--surface); color: var(--text); border: 1px solid var(--line); }
.tabs button.active { background: var(--brand-2); color: #fff; border-color: var(--brand-2); }

/* channel setup helpers */
.field-help {
  font-size: 13px;
  color: var(--muted);
  background: #f1f5f9;
  padding: 10px 12px;
  border-radius: 10px;
  line-height: 1.6;
}
.field-help b { color: var(--text); }
details.help {
  font-size: 13px;
  color: var(--muted);
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
}
details.help summary {
  cursor: pointer;
  color: var(--brand-2);
  font-weight: 600;
}
details.help ol, details.help ul { margin: 8px 0 0; padding-left: 18px; line-height: 1.7; }
details.help code {
  background: #e2e8f0;
  padding: 1px 5px;
  border-radius: 5px;
  font-size: 12px;
}
details.help p { margin: 8px 0 0; }

/* ---- eHandbook event cards ---- */
.event-card {
  position: relative;
  display: block;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.event-card .event-top {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.event-title {
  display: block;
  font-size: 16px;
  line-height: 1.45;
  color: var(--text);
  font-weight: 700;
  word-break: break-word;
}
.event-meta {
  display: block;
  margin-top: 7px;
  color: var(--muted);
  font-size: 12px;
}
.event-card.selectable { padding-left: 48px; }
.event-card .sel-mark {
  position: absolute;
  left: 14px; top: 16px;
  width: 22px; height: 22px;
  border-radius: 50%;
  border: 2px solid var(--line);
  background: var(--surface);
  display: grid; place-items: center;
}
.event-card.selectable .sel-mark { display: grid; }
.event-card.selected {
  border-color: var(--brand-2);
  box-shadow: 0 0 0 2px rgba(22, 163, 74, .25);
}
.event-card.selected .sel-mark {
  background: var(--brand-2);
  border-color: var(--brand-2);
}
.event-card.selected .sel-mark::after {
  content: '✓';
  color: #fff;
  font-size: 13px;
  font-weight: 900;
}

.subject-badge {
  display: inline-block;
  background: var(--subject-color, #64748b);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 6px;
  white-space: nowrap;
}
.kind-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  padding: 4px 9px;
  border-radius: 999px;
  white-space: nowrap;
}
.kind-homework { background: #dbeafe; color: #1d4ed8; }
.kind-reminder { background: #fef3c7; color: #b45309; }
.kind-quiz     { background: #ede9fe; color: #6d28d9; }
.kind-exam     { background: #fee2e2; color: #b91c1c; }
.kind-activity { background: #dcfce7; color: #15803d; }
.kind-other    { background: #e2e8f0; color: #475569; }

.kind-filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.kind-filters button {
  padding: 7px 12px;
  font-size: 12px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--line);
}
.kind-filters button.active { background: var(--brand-2); color: #fff; border-color: var(--brand-2); }

/* ---- image importer ---- */
.import-hero h2 { color: var(--text); }
.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 26px 16px;
  border: 2px dashed #cbd5e1;
  border-radius: var(--radius);
  background: #f8fafc;
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.dropzone.dragging { border-color: var(--brand-2); background: #f0fdf4; }
.dropzone .dz-icon { font-size: 34px; }
.dropzone strong { color: var(--text); }
.dropzone small { color: var(--muted); font-size: 12px; }
.import-options { display: flex; gap: 16px; flex-wrap: wrap; }
.switch-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text);
  font-weight: 600;
}
.switch-row input { width: auto; }
.usage-line { font-size: 12px; color: var(--muted); margin: 4px 0 0; line-height: 1.5; }

.file-item {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 8px;
  background: var(--surface);
}
.file-thumb { width: 54px; height: 54px; object-fit: cover; border-radius: 8px; flex: none; background: #e2e8f0; }
.file-meta { flex: 1; min-width: 0; }
.file-meta strong { display: block; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-meta small { color: var(--muted); font-size: 12px; }
.file-item.has-error { border-color: #fecaca; background: #fef2f2; }
.file-remove { flex: none; background: #e2e8f0; color: #475569; padding: 5px 9px; font-size: 12px; }

.review-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.event-review {
  display: flex;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.event-review.is-dup { border-color: #fdba74; background: #fffbeb; }
.ev-check-wrap { padding-top: 4px; flex: none; }
.ev-check-wrap input { width: 22px; height: 22px; }
.ev-body { flex: 1; min-width: 0; display: grid; gap: 8px; }
.ev-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.ev-row > select, .ev-row > input { flex: 1; min-width: 120px; }
.ev-title { font-weight: 700; }
.ev-notes { min-height: 44px; resize: vertical; }
.ev-tag { font-size: 12px; }
.ev-pill {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  padding: 4px 8px;
  border-radius: 999px;
  white-space: nowrap;
}
.ev-pill.new { background: #dcfce7; color: #15803d; }
.ev-pill.dup { background: #ffedd5; color: #c2410c; }
.ev-pill.warn { background: #fef3c7; color: #b45309; }

.import-summary strong { font-size: 15px; }
.import-done { text-align: center; }
.import-done .done-icon { font-size: 42px; }
.import-done .btn-row { justify-content: center; }

