:root {
  --bg: #1f2933;
  --panel: #2a3441;
  --text: #e7eaee;
  --muted: #9aa5b1;
  --accent: #ebcb8b;
  --danger: #bf616a;
  --ok: #a3be8c;
  --border: #3b4856;
}
* { box-sizing: border-box; }
/* Make the [hidden] attribute actually hide elements whose author CSS
   sets a display value (e.g. .dashboard-grid uses display:grid). Without
   this !important, mode-switching on the dashboard left both panels showing. */
[hidden] { display: none !important; }
html, body {
  margin: 0; padding: 0; background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
}
a { color: var(--accent); text-decoration: none; }
a[aria-current="page"] { color: var(--text); border-bottom: 2px solid var(--accent); padding-bottom: 2px; }

.topbar {
  display: flex; align-items: center; gap: 1rem;
  padding: 0.75rem 1rem; background: var(--panel); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.topbar .brand { font-weight: 700; letter-spacing: 0.5px; }
.topbar nav { display: flex; gap: 1rem; flex: 1; }
.sync-state { font-size: 0.8rem; color: var(--muted); }
.sync-state.syncing { color: var(--accent); }
.sync-state.error   { color: var(--danger); }
.sync-state.ok      { color: var(--ok); }

main { padding: 1rem; max-width: 720px; margin: 0 auto; }
.dashboard-view main { max-width: 1080px; }

h1, h2, h3 { margin-top: 0; }
input[type="text"], input[type="password"] {
  width: 100%; padding: 0.6rem 0.75rem; font-size: 1rem;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

button {
  background: var(--panel); color: var(--text); border: 1px solid var(--border);
  padding: 0.6rem 0.9rem; border-radius: 6px; font-size: 1rem; cursor: pointer;
}
button:hover:not(:disabled) { background: #34404f; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary { background: var(--accent); color: #1f2933; font-weight: 600; border: none; }
button.primary:hover:not(:disabled) { background: #f0d49a; }

.error { color: var(--danger); font-size: 0.9rem; }

/* Recent entries list, shown above the form */
.recent-block { background: var(--panel); border-radius: 8px; padding: 0.75rem 0.75rem 0.5rem; margin-bottom: 1rem; }
.recent-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.4rem; gap: 0.5rem; flex-wrap: wrap; }
.recent-head h3 { margin: 0; font-size: 0.95rem; color: var(--muted); font-weight: 500; }
.recent-head .recent-hint { color: var(--border); font-weight: 400; font-size: 0.8rem; margin-left: 0.35rem; }
.recent-actions { display: flex; gap: 0.4rem; }
.ghost.warn { border-color: var(--accent); color: var(--accent); }
.recent-list {
  list-style: none; padding: 0; margin: 0;
  /* Cap the list so today + yesterday's entries scroll independently of
     the form below, instead of pushing the entry buttons off-screen. */
  max-height: 18rem; overflow-y: auto;
}
.recent-list::-webkit-scrollbar { width: 6px; }
.recent-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
.recent-list li {
  display: grid; grid-template-columns: 4.5rem 1fr auto;
  gap: 0.5rem; padding: 0.45rem 0.4rem; border-radius: 4px;
  cursor: pointer; font-size: 0.95rem;
}
.recent-list li:hover, .recent-list li:focus { background: #34404f; outline: none; }
.recent-list li.editing { background: #3b4856; box-shadow: inset 3px 0 0 var(--accent); }
.recent-list .r-time { color: var(--accent); font-variant-numeric: tabular-nums; }
.recent-list .r-cat { color: var(--text); }
.recent-list .r-detail { color: var(--muted); }
.recent-list .r-dur { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 0.85rem; }
.recent-list .r-newday {
  border-top: 1px dashed var(--border); padding-top: 0.6rem; margin-top: 0.3rem;
}
.recent-list .r-newday::before {
  content: attr(data-date); display: block; color: var(--muted); font-size: 0.75rem;
  margin-bottom: 0.25rem; grid-column: 1 / -1;
}

.editing-banner {
  background: #3b4856; border-left: 3px solid var(--accent);
  padding: 0.5rem 0.75rem; margin-bottom: 0.75rem; border-radius: 4px;
  display: flex; align-items: center; justify-content: space-between;
  font-size: 0.9rem;
}
.editing-banner #editingWhen { font-weight: 600; }

.ghost { background: transparent; border: 1px solid var(--border); padding: 0.3rem 0.6rem; font-size: 0.85rem; }
.ghost:hover { background: #34404f; }

.action-row { display: flex; gap: 0.5rem; align-items: center; }
.action-row #saveBtn { flex: 1; }
button.danger { background: var(--danger); color: white; border: none; padding: 0.7rem 1rem; }
button.danger:hover { background: #d97781; }

/* Entry screen */
.time-row { display: grid; grid-template-columns: 3.5rem 1fr auto; gap: 0.5rem; align-items: center; margin-bottom: 0.5rem; }
.time-row label { color: var(--muted); font-size: 0.9rem; }
.time-row #nowBtn, .time-row #todayBtn { padding: 0.5rem 0.8rem; }
.time-row input[type="date"] {
  font-family: inherit; color-scheme: dark;
  /* Use a slightly different shade so it's visually distinct from time */
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: 0.55rem 0.6rem; font-size: 1rem;
}
.time-row input[type="date"].past { border-color: var(--accent); }
.quick-times { display: flex; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; flex-wrap: wrap; }
.qt { padding: 0.35rem 0.6rem; font-size: 0.85rem; }
.time-hint { color: var(--muted); font-size: 0.85rem; margin-left: auto; }
.time-hint.future { color: var(--accent); }

.cat-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.5rem; margin: 0.5rem 0 1rem;
}
@media (min-width: 600px) { .cat-grid { grid-template-columns: repeat(4, 1fr); } }
.cat-btn {
  padding: 1rem 0.5rem; font-size: 1rem; border: 1px solid var(--border);
  background: var(--panel); border-radius: 8px; text-align: center; cursor: pointer;
  display: flex; align-items: center; justify-content: center; min-height: 56px;
}
.cat-btn.selected { background: var(--accent); color: #1f2933; border-color: var(--accent); font-weight: 600; }

.detail-row { position: relative; margin: 0.5rem 0 1rem; }
.detail-row label { display: block; color: var(--muted); font-size: 0.9rem; margin-bottom: 0.25rem; }
.suggestions {
  list-style: none; padding: 0; margin: 0.25rem 0 0;
  background: var(--panel); border: 1px solid var(--border); border-radius: 6px;
  max-height: 180px; overflow-y: auto;
}
.suggestions:empty { display: none; }
.suggestions li { padding: 0.5rem 0.75rem; cursor: pointer; border-bottom: 1px solid var(--border); }
.suggestions li:last-child { border-bottom: none; }
.suggestions li:hover, .suggestions li.active { background: #34404f; }
.suggestions .freq { color: var(--muted); font-size: 0.85rem; margin-left: 0.5rem; }

#saveBtn { width: 100%; padding: 0.9rem; font-size: 1.05rem; }

/* Sign-in */
#signin { max-width: 360px; margin: 3rem auto; text-align: center; }
#signin h1 { margin-bottom: 0.5rem; }
#signin p { color: var(--muted); }
#signin input { margin-bottom: 0.75rem; }
#signin button { width: 100%; }

/* First-login onboarding overlay */
.onboarding-overlay {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0, 0, 0, 0.7);
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
}
.onboarding-card {
  max-width: 460px; width: 100%;
  background: var(--panel); border-radius: 10px;
  padding: 1.5rem 1.5rem 1.25rem;
  box-shadow: 0 12px 40px rgba(0,0,0,0.5);
  max-height: 90vh; overflow-y: auto;
}
.onboarding-card h2 { margin: 0 0 0.5rem; }
.onboarding-card p { margin: 0 0 0.75rem; }
.onboarding-card ul { padding-left: 1.2rem; margin: 0.5rem 0 1rem; }
.onboarding-card li { margin-bottom: 0.5rem; }
.onboarding-card .muted { color: var(--muted); font-size: 0.9rem; }
.onboarding-card .onboarding-dismiss {
  display: flex; align-items: center; gap: 0.5rem;
  color: var(--muted); font-size: 0.9rem; margin: 1rem 0 0.75rem;
  cursor: pointer;
}
.onboarding-card button { width: 100%; padding: 0.8rem; }
.archive-confirm-buttons {
  display: flex; gap: 0.5rem; margin-top: 0.25rem;
}
.archive-confirm-buttons button { width: 100%; padding: 0.7rem; margin-top: 0; }

/* Bigger drag handle for mobile fingers + stronger visual cue. */
.cat-edit-row .drag-handle {
  min-width: 2.2rem; min-height: 2.2rem;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 1.1rem; line-height: 1;
}
.cat-drag-item .drag-handle {
  min-width: 1.6rem; min-height: 1.6rem;
  display: inline-flex; align-items: center; justify-content: center;
}

/* Month nav (parallel to week-nav) */
.month-nav { display: flex; gap: 0.5rem; align-items: center; flex: 1; min-width: 12rem; }
.month-nav #monthLabel { font-weight: 600; flex: 1; text-align: center; }

/* Dashboard */
.controls { display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap; margin-bottom: 1rem; }
.controls .week-nav { display: flex; gap: 0.5rem; align-items: center; flex: 1; min-width: 12rem; }
.controls #weekLabel { font-weight: 600; flex: 1; text-align: center; }
.mode-toggle { display: inline-flex; background: var(--panel); border-radius: 6px; padding: 2px; }
.mode-btn { background: transparent; border: none; padding: 0.45rem 0.8rem; font-size: 0.9rem; color: var(--muted); border-radius: 4px; }
.mode-btn.active { background: var(--accent); color: #1f2933; font-weight: 600; }
.dashboard-grid {
  display: grid; grid-template-columns: 1fr; gap: 1rem;
  margin-bottom: 1.5rem;
}
@media (min-width: 768px) { .dashboard-grid { grid-template-columns: 1.4fr 1fr; } }
.chart-wrap { background: var(--panel); border-radius: 8px; padding: 1rem; }
.chart-title { margin: 0 0 0.75rem; font-size: 1rem; color: var(--muted); font-weight: 500; }
/* Canvas-holder gives Chart.js a fixed-height relative parent so its
   "responsive: true" sizing has something definite to size against —
   otherwise the chart grows the parent which grows the chart and so on. */
.canvas-holder { position: relative; height: 360px; width: 100%; }
.canvas-holder canvas { max-width: 100%; }
.totals { background: var(--panel); border-radius: 8px; padding: 1rem; }
.totals h3 { margin-top: 0; }
.total-row { display: flex; justify-content: space-between; padding: 0.3rem 0; border-bottom: 1px dashed var(--border); }
.total-row:last-child { border-bottom: none; }
.total-row .cat { display: flex; align-items: center; gap: 0.4rem; }
.total-row .swatch { width: 12px; height: 12px; border-radius: 2px; display: inline-block; }
.total-row .sub { color: var(--muted); font-size: 0.85rem; padding-left: 1.4rem; }

.entries-list { margin-top: 1.5rem; }
.entries-list table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.entries-list th, .entries-list td { padding: 0.4rem 0.6rem; border-bottom: 1px solid var(--border); text-align: left; }
.entries-list tbody tr:hover { background: #34404f; }

/* Entry-grid cat-btn swatch: a thin tinted left border so each button is
   colour-keyed to its dashboard slice without overwhelming the selected state. */
.cat-btn.has-swatch { border-left: 4px solid var(--cat-color, var(--border)); }
.cat-btn.has-swatch.selected { border-left-color: #1f2933; }

/* === Draggable category totals list (dashboard) === */
.cat-drag-list {
  list-style: none; padding: 0; margin: 0;
  display: flex; flex-direction: column; gap: 1px;
}
.cat-drag-item {
  display: grid;
  grid-template-columns: 1.2rem 0.9rem 1fr auto;
  align-items: center; gap: 0.5rem;
  padding: 0.45rem 0.3rem;
  border-radius: 4px;
  font-size: 0.95rem;
  background: transparent;
  transition: background 0.1s;
}
.cat-drag-item:hover { background: #34404f; }
.cat-drag-item.sub {
  padding-left: 0.3rem; font-size: 0.85rem; color: var(--muted);
}
.cat-drag-item .drag-handle {
  color: var(--muted);
  cursor: grab; user-select: none;
  text-align: center; font-size: 0.9rem;
  letter-spacing: -0.15rem;
}
.cat-drag-item .drag-handle:active { cursor: grabbing; }
.cat-drag-item .drag-handle.spacer { cursor: default; }
.cat-drag-item .swatch {
  width: 12px; height: 12px; border-radius: 2px; display: inline-block;
}
.cat-drag-item .swatch.indent { background: transparent; }
.cat-drag-item .name { color: var(--text); }
.cat-drag-item.orphan .name { color: var(--muted); font-style: italic; }
.cat-drag-item .time {
  color: var(--muted); font-variant-numeric: tabular-nums; font-size: 0.85rem;
  display: flex; flex-direction: column; align-items: flex-end; gap: 1px;
  line-height: 1.15;
}
.cat-drag-item .time strong { color: var(--text); font-weight: 600; font-size: 0.95rem; }
.cat-drag-item .time .avg {
  font-size: 0.72rem; color: var(--muted);
}
.cat-drag-item.sub .time strong { font-weight: 500; color: var(--muted); font-size: 0.85rem; }

/* Totals panel header tweaks — sub-head + grouped rows w/ avg-per-day. */
.totals-subhead {
  color: var(--muted); font-size: 0.8rem;
  margin: -0.5rem 0 0.75rem; font-style: italic;
}
.total-row.group .amt {
  display: flex; align-items: baseline; gap: 0.4rem;
  font-variant-numeric: tabular-nums;
}
.total-row.group .amt .avg { color: var(--muted); font-size: 0.8rem; }
.cat-drag-item.sortable-ghost { opacity: 0.4; }
.cat-drag-item.sortable-chosen { background: #3b4856; }

/* === Settings page === */
.settings-view main { max-width: 720px; }
.settings-section { background: var(--panel); border-radius: 8px; padding: 1rem 1.2rem; margin-bottom: 1.5rem; }
.settings-head { display: flex; align-items: baseline; justify-content: space-between; gap: 1rem; }
.settings-head h2 { margin: 0; }
.cat-count { color: var(--muted); font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.cat-count.full { color: var(--accent); font-weight: 600; }
.settings-section .hint { color: var(--muted); font-size: 0.9rem; }

.cat-edit-list { list-style: none; padding: 0; margin: 0.75rem 0; }
.cat-edit-row {
  display: grid;
  grid-template-columns: 1.2rem 1.6rem 1fr auto;
  gap: 0.6rem; align-items: center;
  padding: 0.55rem 0.4rem;
  border-radius: 4px;
  background: #2a3441;
  margin-bottom: 4px;
}
.cat-edit-row:hover { background: #34404f; }
.cat-edit-row.archived { opacity: 0.75; }
.cat-edit-row .drag-handle {
  color: var(--muted); cursor: grab; user-select: none;
  text-align: center; letter-spacing: -0.15rem;
}
.cat-edit-row .drag-handle:active { cursor: grabbing; }
.cat-edit-row .cat-color-swatch {
  width: 22px; height: 22px; border-radius: 4px; border: 1px solid var(--border);
  cursor: pointer; padding: 0;
}
.cat-edit-row .cat-color-swatch:hover { outline: 2px solid var(--accent); outline-offset: 1px; }
.cat-edit-row .cat-name { color: var(--text); font-size: 0.95rem; }
.cat-edit-row .cat-actions { display: flex; gap: 0.4rem; }
.cat-edit-row.sortable-ghost { opacity: 0.4; }
.cat-edit-row.sortable-chosen { background: #3b4856; }
.cat-edit-empty { color: var(--muted); padding: 0.5rem; font-size: 0.9rem; }

.add-cat-row { display: flex; gap: 0.5rem; margin-top: 0.75rem; }
.add-cat-row input { flex: 1; }

.archived-block { margin-top: 1.25rem; }
.archived-block summary { cursor: pointer; color: var(--muted); padding: 0.4rem 0; user-select: none; }
.archived-block[open] summary { color: var(--text); }
.cat-edit-list.archived .cat-edit-row { background: #242c36; }

/* Color palette popover */
.palette-popover {
  position: absolute; z-index: 100;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  padding: 0.5rem; width: 200px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}
.palette-swatches {
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 4px;
}
.palette-swatch {
  width: 100%; aspect-ratio: 1; padding: 0;
  border: 1px solid rgba(0,0,0,0.3); border-radius: 4px; cursor: pointer;
}
.palette-swatch:hover { outline: 2px solid var(--accent); outline-offset: 1px; }
