:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --primary: #1e3a5f;
  --primary-hover: #162c49;
  --primary-soft: #e8eff8;
  --accent: #d97706;
  --accent-soft: #fef3c7;
  --success: #059669;
  --success-soft: #d1fae5;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --info: #0284c7;
  --info-soft: #e0f2fe;
  --shadow: 0 2px 10px rgba(15, 23, 42, 0.06);
  --radius: 12px;
  --sidebar-w: 240px;
  --bottom-nav-h: 64px;
}

[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #131c31;
  --surface-alt: #1b2640;
  --border: #263554;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --primary-soft: rgba(59, 130, 246, 0.16);
  --accent: #f59e0b;
  --accent-soft: rgba(245, 158, 11, 0.16);
  --success: #10b981;
  --success-soft: rgba(16, 185, 129, 0.16);
  --danger: #f87171;
  --danger-soft: rgba(248, 113, 113, 0.16);
  --info: #38bdf8;
  --info-soft: rgba(56, 189, 248, 0.16);
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "PingFang HK", "Microsoft JhengHei", "Noto Sans TC", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.num, .tabular {
  font-variant-numeric: tabular-nums;
}

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

/* Layout */
.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 30;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 8px 18px;
  border-bottom: 1px solid var(--border);
}

.brand-icon {
  width: 38px;
  height: 38px;
  border-radius: 10px;
}

.brand-title {
  font-weight: 800;
  font-size: 1.05rem;
  letter-spacing: 0.3px;
}

.brand-sub {
  font-size: 0.74rem;
  color: var(--text-muted);
}

.side-nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 14px;
  flex: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 0.94rem;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s, color 0.15s;
}

.nav-item:hover {
  background: var(--surface-alt);
  color: var(--text);
}

.nav-item.active {
  background: var(--primary-soft);
  color: var(--primary);
}

.nav-ico {
  font-size: 1.08rem;
}

.nav-badge {
  margin-left: auto;
  background: var(--danger);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 999px;
}

.sidebar-footer {
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.ai-status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
}

.ai-status-pill.gemini-on {
  background: var(--success-soft);
  color: var(--success);
  border-color: transparent;
}

.main-wrap {
  flex: 1;
  margin-left: var(--sidebar-w);
  display: flex;
  flex-direction: column;
  min-width: 0;
}

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

.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  flex: 1;
}

.mobile-brand {
  display: none;
  align-items: center;
  gap: 8px;
}

.mobile-brand-icon {
  width: 28px;
  height: 28px;
  border-radius: 7px;
}

.mobile-brand-title {
  font-weight: 800;
  font-size: 0.96rem;
  white-space: nowrap;
}

.project-picker-wrap {
  max-width: 300px;
  width: 100%;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.view-root {
  padding: 20px 24px 40px;
  max-width: 1280px;
  width: 100%;
  margin: 0 auto;
}

/* Reminder Banner */
.reminder-banner {
  background: var(--accent-soft);
  border-bottom: 1px solid var(--accent);
  padding: 10px 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.reminder-banner.overdue {
  background: var(--danger-soft);
  border-color: var(--danger);
}

/* Cards & Grids */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.card-title {
  font-size: 1.05rem;
  font-weight: 700;
  margin: 0;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}

.page-title {
  font-size: 1.4rem;
  font-weight: 800;
  margin: 0;
}

.grid-4 {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
}

.grid-3 {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}

.grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.stack-sm { display: flex; flex-direction: column; gap: 8px; }
.stack-md { display: flex; flex-direction: column; gap: 14px; }
.stack-lg { display: flex; flex-direction: column; gap: 20px; }
.row-wrap { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }

/* KPI */
.kpi-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.kpi-label {
  font-size: 0.82rem;
  color: var(--text-muted);
  font-weight: 600;
}

.kpi-value {
  font-size: 1.55rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.kpi-sub {
  font-size: 0.78rem;
  color: var(--text-muted);
}

/* Inputs & Buttons */
.input, .select, .textarea, .select-compact {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 0.94rem;
  outline: none;
  transition: border-color 0.15s;
}

.select-compact {
  padding: 7px 10px;
  font-size: 0.88rem;
}

.input:focus, .select:focus, .textarea:focus, .select-compact:focus {
  border-color: var(--primary);
}

.textarea {
  resize: vertical;
  min-height: 90px;
}

.field-label {
  display: block;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 5px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 15px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  text-decoration: none;
  transition: transform 0.08s, background 0.15s, border-color 0.15s;
}

.btn:hover { background: var(--surface-alt); }
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-hover); }

.btn-accent {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-success {
  background: var(--success);
  border-color: var(--success);
  color: #fff;
}

.btn-danger {
  background: var(--danger-soft);
  border-color: transparent;
  color: var(--danger);
}

.btn-sm {
  padding: 5px 10px;
  font-size: 0.8rem;
  border-radius: 7px;
}

.btn-block { width: 100%; }

.icon-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 1rem;
}

.bell-badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background: var(--danger);
  color: #fff;
  font-size: 0.68rem;
  font-weight: 800;
  min-width: 18px;
  height: 18px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
}

/* Pills & Badges */
.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
}

.pill-primary { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.pill-accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.pill-success { background: var(--success-soft); color: var(--success); border-color: transparent; }
.pill-danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.pill-info { background: var(--info-soft); color: var(--info); border-color: transparent; }

/* Tabs */
.tabs {
  display: inline-flex;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
  gap: 4px;
  flex-wrap: wrap;
}

.tab-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 7px 14px;
  border-radius: 7px;
  font-family: inherit;
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
}

.tab-btn.active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

/* Progress Bar */
.progress-track {
  width: 100%;
  height: 9px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 999px;
  transition: width 0.3s;
}
.progress-fill.warn { background: var(--accent); }
.progress-fill.over { background: var(--danger); }

/* Tables */
.table-wrap {
  width: 100%;
  overflow-x: auto;
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

.table th, .table td {
  text-align: left;
  padding: 10px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.table th {
  font-size: 0.78rem;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 700;
  background: var(--surface-alt);
}

.table tr:hover td {
  background: var(--surface-alt);
}

/* Dropzone */
.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 22px;
  text-align: center;
  background: var(--surface-alt);
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}

.dropzone.dragover {
  border-color: var(--primary);
  background: var(--primary-soft);
}

/* Gallery Thumbs */
.thumb-img {
  width: 54px;
  height: 54px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
  cursor: pointer;
}

.media-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 12px;
}

.media-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--surface);
  display: flex;
  flex-direction: column;
}

.media-preview {
  height: 130px;
  background: var(--surface-alt);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  cursor: pointer;
}

.media-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Modal & Lightbox */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 60;
}

.modal-dialog {
  width: 100%;
  max-width: 620px;
  max-height: 90vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
}

.modal-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h3 { margin: 0; font-size: 1.08rem; }

.modal-body {
  padding: 18px;
  overflow-y: auto;
}

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.88);
  z-index: 80;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.lightbox img {
  max-width: 92vw;
  max-height: 80vh;
  border-radius: 8px;
}

.lightbox-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 700;
}

.lightbox-caption {
  color: #e2e8f0;
  margin-top: 10px;
  font-size: 0.9rem;
}

.auth-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 90;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.auth-card {
  width: 100%;
  max-width: 390px;
}

.auth-logo {
  font-size: 1.4rem;
  font-weight: 800;
  margin-bottom: 6px;
}

/* Toasts */
.toast-container {
  position: fixed;
  bottom: 22px;
  right: 22px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  background: #0f172a;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 0.88rem;
  font-weight: 600;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.25);
}

.toast.success { background: #065f46; }
.toast.error { background: #991b1b; }

/* Helpers */
.text-muted { color: var(--text-muted); }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-accent { color: var(--accent); }
.text-sm { font-size: 0.84rem; }
.text-xs { font-size: 0.75rem; }
.font-bold { font-weight: 700; }

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

.show-sm { display: none; }

/* Mobile Bottom Nav */
.bottom-nav {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: var(--bottom-nav-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 35;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.bnav-item {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-family: inherit;
  font-size: 0.7rem;
  font-weight: 700;
  cursor: pointer;
}

.bnav-item span:first-child { font-size: 1.2rem; }
.bnav-item.active { color: var(--primary); }

@media (max-width: 980px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 768px) {
  .sidebar { display: none; }
  .main-wrap { margin-left: 0; padding-bottom: calc(var(--bottom-nav-h) + 14px); }
  .mobile-brand { display: flex; }
  .bottom-nav { display: flex; }
  .hide-sm { display: none; }
  .show-sm { display: inline-flex; }
  .topbar { padding: 8px 12px; }
  .view-root { padding: 14px 12px 26px; }
  .grid-4, .grid-3, .grid-2 { grid-template-columns: 1fr; }
  .toast-container { bottom: 76px; right: 12px; left: 12px; }
}

@media print {
  .sidebar, .topbar, .bottom-nav, .reminder-banner, .no-print { display: none !important; }
  .main-wrap { margin-left: 0; }
  .card { box-shadow: none; break-inside: avoid; }
}
