:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efece5;
  --surface-hover: #e7e3da;
  --text: #1a1a1d;
  --text-dim: #6f6b74;
  --border: #e2ddd2;
  --gold: #b8892f;
  --gold-dark: #8f6a22;
  --gold-soft: rgba(184,137,47,.13);
  --red: #b3261e;
  --red-soft: rgba(179,38,30,.12);
  --green: #2e7d32;
  --shadow-sm: 0 1px 2px rgba(20,18,12,.05), 0 1px 3px rgba(20,18,12,.06);
  --shadow-md: 0 10px 24px rgba(20,18,12,.08), 0 2px 8px rgba(20,18,12,.05);
  --shadow-lg: 0 24px 60px rgba(20,18,12,.16), 0 8px 20px rgba(20,18,12,.08);
  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 20px;
  --font-scale: 1;
  --ease: cubic-bezier(.22,.7,.32,1);
}
[data-theme="dark"] {
  --bg: #0a0a0c;
  --surface: #17171b;
  --surface-2: #1f1f24;
  --surface-hover: #28282e;
  --text: #f3f1ec;
  --text-dim: #9c98a3;
  --border: #2b2b31;
  --gold: #dcb455;
  --gold-dark: #b8892f;
  --gold-soft: rgba(220,180,85,.16);
  --red-soft: rgba(224,90,82,.16);
  --shadow-sm: 0 1px 2px rgba(0,0,0,.35);
  --shadow-md: 0 10px 28px rgba(0,0,0,.5);
  --shadow-lg: 0 30px 70px rgba(0,0,0,.6);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0a0a0c; --surface: #17171b; --surface-2: #1f1f24; --surface-hover: #28282e;
    --text: #f3f1ec; --text-dim: #9c98a3; --border: #2b2b31;
    --gold: #dcb455; --gold-dark: #b8892f; --gold-soft: rgba(220,180,85,.16); --red-soft: rgba(224,90,82,.16);
    --shadow-sm: 0 1px 2px rgba(0,0,0,.35); --shadow-md: 0 10px 28px rgba(0,0,0,.5); --shadow-lg: 0 30px 70px rgba(0,0,0,.6);
  }
}

* { box-sizing: border-box; }
html { font-size: calc(15px * var(--font-scale)); scroll-behavior: smooth; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button { font-family: inherit; cursor: pointer; }
input, select { font-family: inherit; font-size: 1rem; }
button:focus-visible, input:focus-visible, a:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--gold); outline-offset: 2px;
}
::selection { background: var(--gold-soft); }

/* ---------- Auth screen ---------- */
.auth-screen {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px;
  background:
    radial-gradient(circle at 15% -10%, var(--gold-soft), transparent 55%),
    radial-gradient(circle at 100% 110%, var(--gold-soft), transparent 45%),
    var(--bg);
  position: relative;
}
.auth-screen[hidden] { display: none; }
.auth-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg); padding: 40px 36px; width: 100%; max-width: 400px;
  position: relative; overflow: hidden;
  animation: rise .5s var(--ease);
}
.auth-card::before {
  content: ""; position: absolute; top: 0; left: 0; right: 0; height: 4px;
  background: linear-gradient(90deg, var(--gold-dark), var(--gold), var(--gold-dark));
}
@keyframes rise { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
.brand { display: flex; align-items: center; gap: 14px; margin-bottom: 28px; }
.brand-mark {
  display: inline-flex; align-items: center; justify-content: center;
  width: 46px; height: 46px; border-radius: 12px;
  background: linear-gradient(150deg, #1c1c20, #08080a);
  color: var(--gold); font-weight: 700; font-size: 22px; font-family: "Fraunces", serif; flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}
.brand-mark.small { width: 32px; height: 32px; font-size: 16px; border-radius: 9px; }
.brand h1 { font-family: "Fraunces", serif; font-weight: 700; font-size: 1.5rem; margin: 0; letter-spacing: -.01em; }
.tagline { margin: 3px 0 0; color: var(--text-dim); font-size: .84rem; }
#login-form label, #signup-form label, #reset-form label, #new-password-form label {
  display: block; margin-bottom: 16px; font-size: .82rem; color: var(--text-dim); font-weight: 500;
}
#login-form input, #signup-form input, #reset-form input, #new-password-form input {
  display: block; width: 100%; margin-top: 7px; padding: 11px 13px;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2); color: var(--text);
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease), background .15s var(--ease);
}
#login-form input:focus, #signup-form input:focus, #reset-form input:focus, #new-password-form input:focus {
  outline: none; border-color: var(--gold); box-shadow: 0 0 0 3px var(--gold-soft); background: var(--surface);
}
.btn-primary {
  width: 100%; padding: 12px; border: none; border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--gold), var(--gold-dark)); color: #17130a; font-weight: 700; font-size: .95rem;
  box-shadow: var(--shadow-sm); transition: transform .12s var(--ease), box-shadow .15s var(--ease), filter .15s var(--ease);
}
.btn-primary:hover { filter: brightness(1.06); box-shadow: var(--shadow-md); }
.btn-primary:active { transform: scale(.98); }
.btn-secondary {
  padding: 10px 15px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text); font-size: .9rem; font-weight: 500;
  transition: background .15s var(--ease), border-color .15s var(--ease), transform .12s var(--ease);
}
.btn-secondary:hover { background: var(--surface-hover); }
.btn-secondary:active { transform: scale(.98); }
.btn-link { background: none; border: none; color: var(--gold-dark); font-size: .85rem; text-decoration: underline; }
[data-theme="dark"] .btn-link { color: var(--gold); }
.auth-links { display: flex; justify-content: space-between; margin-top: 16px; }
.auth-links button { background: none; border: none; color: var(--text-dim); font-size: .8rem; text-decoration: underline; transition: color .15s var(--ease); }
.auth-links button:hover { color: var(--text); }
.auth-error { color: var(--red); font-size: .85rem; margin-top: 10px; }
.auth-note { color: var(--text-dim); font-size: .8rem; margin-top: 10px; line-height: 1.45; }

/* ---------- App shell ---------- */
.app-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10; flex-wrap: wrap; gap: 10px;
  box-shadow: var(--shadow-sm);
}
.header-left { display: flex; align-items: center; gap: 11px; }
.app-title { font-family: "Fraunces", serif; font-weight: 700; font-size: 1.08rem; letter-spacing: -.01em; }
.header-right { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.text-size-control { display: flex; align-items: center; gap: 6px; font-size: .8rem; color: var(--text-dim); }
.text-size-control button, .icon-btn {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  transition: background .15s var(--ease), transform .12s var(--ease), border-color .15s var(--ease);
}
.text-size-control button { width: 27px; height: 27px; }
.icon-btn { width: 32px; height: 32px; }
.text-size-control button:hover, .icon-btn:hover { background: var(--surface-hover); border-color: var(--gold); }
.text-size-control button:active, .icon-btn:active { transform: scale(.92); }
.user-email { color: var(--text-dim); font-size: .8rem; }

.tabs { display: flex; gap: 5px; padding: 10px 20px; background: var(--surface); overflow-x: auto; border-bottom: 1px solid var(--border); }
.tab {
  background: none; border: none; padding: 9px 16px; border-radius: 999px;
  color: var(--text-dim); font-size: .87rem; font-weight: 500; white-space: nowrap;
  transition: background .15s var(--ease), color .15s var(--ease);
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab.active { color: #17130a; background: var(--gold); font-weight: 700; box-shadow: var(--shadow-sm); }
.tab.active:hover { background: var(--gold); }

main { padding: 22px 18px 60px; max-width: 900px; margin: 0 auto; }
.tab-panel { display: none; }
.tab-panel.active { display: block; animation: fadein .25s var(--ease); }
@keyframes fadein { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ---------- Timer ---------- */
.timer-wrap { text-align: center; padding: 26px 0 10px; }
.timer-ring {
  width: min(280px, 62vw); aspect-ratio: 1; margin: 0 auto 30px; border-radius: 50%;
  background: conic-gradient(var(--gold) calc(var(--progress, 0) * 1%), var(--surface-2) 0);
  display: flex; align-items: center; justify-content: center; position: relative;
  box-shadow: var(--shadow-md); transition: background .6s linear;
}
.timer-ring.break { background: conic-gradient(var(--green) calc(var(--progress, 0) * 1%), var(--surface-2) 0); }
.timer-ring::after {
  content: ""; position: absolute; inset: 13px; border-radius: 50%; background: var(--surface);
  box-shadow: inset 0 2px 10px rgba(0,0,0,.08);
}
.timer-ring-inner { position: relative; z-index: 1; }
.timer-display { font-family: "Fraunces", serif; font-size: 2.9rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1; }
.timer-mode { color: var(--gold-dark); text-transform: uppercase; letter-spacing: .14em; font-size: .78rem; margin-top: 8px; font-weight: 700; }
[data-theme="dark"] .timer-mode { color: var(--gold); }
.timer-ring.break .timer-mode { color: var(--green); }
#timer-task-input {
  display: block; margin: 0 auto 22px; padding: 11px 15px; width: 100%; max-width: 340px;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2); color: var(--text); text-align: center;
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease), background .15s var(--ease);
}
#timer-task-input:focus { outline: none; border-color: var(--gold); box-shadow: 0 0 0 3px var(--gold-soft); background: var(--surface); }
.timer-controls { display: flex; gap: 10px; justify-content: center; margin-bottom: 20px; }
.timer-controls button { min-width: 112px; }
.checkbox-row { display: flex; align-items: center; gap: 8px; justify-content: center; font-size: .85rem; color: var(--text-dim); }
.checkbox-row.inline { justify-content: flex-start; }
.today-stats { margin-top: 20px; color: var(--text-dim); font-size: .9rem; display: flex; gap: 16px; justify-content: center; }
#streak-display { color: var(--gold-dark); font-weight: 600; }
[data-theme="dark"] #streak-display { color: var(--gold); }

/* ---------- Toolbar / search ---------- */
.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.search-box { position: relative; flex: 1; min-width: 200px; }
.search-icon { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--text-dim); font-size: .95rem; }
.search-box input {
  width: 100%; padding: 10px 34px 10px 32px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text); transition: border-color .15s var(--ease), box-shadow .15s var(--ease), background .15s var(--ease);
}
.search-box input:focus { outline: none; border-color: var(--gold); box-shadow: 0 0 0 3px var(--gold-soft); background: var(--surface); }
.clear-btn {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  width: 22px; height: 22px; border: none; border-radius: 50%; background: var(--border);
  color: var(--text); font-size: .95rem; line-height: 1; display: flex; align-items: center; justify-content: center;
  transition: background .15s var(--ease), color .15s var(--ease);
}
.clear-btn:hover { background: var(--gold); color: #17130a; }
#view-switcher { padding: 9px 11px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface-2); color: var(--text); }
.hint { font-size: .8rem; color: var(--gold-dark); margin: 0 0 10px; font-weight: 500; }
[data-theme="dark"] .hint { color: var(--gold); }

/* ---------- Task list ---------- */
.task-list { display: flex; flex-direction: column; gap: 4px; }
.task-row {
  display: flex; align-items: flex-start; gap: 10px; padding: 12px 14px;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); margin-bottom: 4px;
  transition: box-shadow .15s var(--ease), border-color .15s var(--ease), transform .12s var(--ease);
}
.task-row:hover { box-shadow: var(--shadow-sm); border-color: var(--gold-soft); }
.task-row.dragging { opacity: .4; }
.task-row.drop-target { outline: 2px dashed var(--gold); }
.task-row .indent { flex-shrink: 0; }
.task-row .drag-handle { cursor: grab; color: var(--text-dim); }
.task-row .task-main { flex: 1; min-width: 0; }
.task-row .task-name { font-weight: 600; }
.task-row .task-name.done { text-decoration: line-through; color: var(--text-dim); font-weight: 500; }
.task-row .task-meta { font-size: .75rem; color: var(--text-dim); margin-top: 3px; display: flex; gap: 10px; flex-wrap: wrap; }
.task-row .overdue { color: var(--red); font-weight: 600; }
.task-row .rollup { font-size: .75rem; color: var(--gold-dark); font-weight: 600; }
[data-theme="dark"] .task-row .rollup { color: var(--gold); }
.task-row .row-actions { display: flex; gap: 4px; flex-shrink: 0; }
.task-row .row-actions button {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  width: 29px; height: 29px; font-size: .85rem; transition: background .15s var(--ease), border-color .15s var(--ease);
}
.task-row .row-actions button:hover { background: var(--surface-hover); border-color: var(--gold); }
.attach-flag { color: var(--text-dim); font-size: .75rem; }

/* ---------- Modal / edit dialog ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(10,10,12,.55); backdrop-filter: blur(3px); display: flex;
  align-items: center; justify-content: center; z-index: 100; padding: 16px;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--surface); border-radius: var(--radius-lg); width: 100%; max-width: 640px;
  max-height: 90vh; display: flex; flex-direction: column; box-shadow: var(--shadow-lg);
  animation: rise .3s var(--ease); border: 1px solid var(--border);
}
.modal.maximized { max-width: 98vw; max-height: 98vh; height: 98vh; }
.modal-header { display: flex; align-items: center; gap: 8px; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-header h2 { flex: 1; margin: 0; font-family: "Fraunces", serif; font-size: 1.15rem; }
.subtask-of { padding: 0 20px; margin: 10px 0 0; color: var(--gold-dark); font-size: .85rem; font-weight: 500; }
[data-theme="dark"] .subtask-of { color: var(--gold); }
.modal-body { padding: 16px 20px; overflow-y: auto; flex: 1; }
.modal-body label { display: block; font-size: .8rem; color: var(--text-dim); margin-bottom: 14px; font-weight: 500; }
.modal-body input[type=text], .modal-body input[type=date], .modal-body input[type=number] {
  display: block; width: 100%; margin-top: 6px; padding: 10px 11px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); background: var(--surface-2); color: var(--text);
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease), background .15s var(--ease);
}
.modal-body input:focus { outline: none; border-color: var(--gold); box-shadow: 0 0 0 3px var(--gold-soft); background: var(--surface); }
.field-row { display: flex; gap: 12px; }
.field-row label { flex: 1; }
.modal-footer { display: flex; justify-content: flex-end; gap: 10px; padding: 16px 20px; border-top: 1px solid var(--border); }

.rte-toolbar { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
.rte-toolbar button {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  min-width: 31px; height: 31px; font-size: .85rem; transition: background .15s var(--ease), border-color .15s var(--ease);
}
.rte-toolbar button:hover { background: var(--surface-hover); border-color: var(--gold); }
.rte-editor {
  min-height: 140px; border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 11px 13px;
  background: var(--surface-2); color: var(--text); overflow-y: auto; max-height: 300px;
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease);
}
.rte-editor:focus { outline: none; border-color: var(--gold); box-shadow: 0 0 0 3px var(--gold-soft); }
.rte-editor img { max-width: 100%; border-radius: var(--radius-sm); margin: 4px 0; }
.attachments-block { margin-top: 16px; }
#attachments-list { list-style: none; padding: 0; margin: 6px 0; display: flex; flex-direction: column; gap: 5px; }
#attachments-list li { display: flex; justify-content: space-between; align-items: center; font-size: .85rem; background: var(--surface-2); padding: 7px 11px; border-radius: var(--radius-sm); }
#attachments-list button { background: none; border: none; color: var(--red); }

/* ---------- Calendar ---------- */
.calendar-header { display: flex; align-items: center; gap: 10px; margin: 16px 0; }
.calendar-header h2 { flex: 1; margin: 0; font-family: "Fraunces", serif; font-size: 1.15rem; text-align: center; }
.calendar-body { display: flex; gap: 20px; flex-wrap: wrap; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 5px; flex: 2; min-width: 280px; }
.cal-cell {
  border: 1px solid var(--border); border-radius: var(--radius-sm); min-height: 64px; padding: 5px 7px;
  font-size: .72rem; background: var(--surface); cursor: pointer;
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease), transform .12s var(--ease);
}
.cal-cell:hover { border-color: var(--gold); box-shadow: var(--shadow-sm); }
.cal-cell.today { outline: 2px solid var(--gold); box-shadow: 0 0 0 4px var(--gold-soft); }
.cal-cell.selected { outline: 2px solid var(--text); }
.cal-cell.empty { background: transparent; border-color: transparent; cursor: default; }
.cal-cell.empty:hover { box-shadow: none; }
.cal-cell .day-num { font-weight: 700; }
.cal-cell .cal-task { color: var(--text-dim); display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cal-cell .cal-task.overdue { color: var(--red); }
.cal-cell .cal-badge { color: var(--green); font-size: .7rem; font-weight: 700; }
.cal-day-panel { flex: 1; min-width: 220px; }
.cal-day-panel h3 { font-family: "Fraunces", serif; font-size: 1.05rem; }
.cal-day-panel ul { list-style: none; padding: 0; }
.cal-day-panel li { padding: 9px 11px; border: 1px solid var(--border); border-radius: var(--radius-sm); margin-bottom: 6px; cursor: pointer; font-size: .85rem; transition: border-color .15s var(--ease), background .15s var(--ease); }
.cal-day-panel li:hover { background: var(--surface-2); border-color: var(--gold); }

.heatmap .cal-cell { min-height: 46px; }

/* ---------- Dashboard ---------- */
.stat-row { display: flex; gap: 14px; flex-wrap: wrap; margin-bottom: 24px; }
.stat-box {
  flex: 1; min-width: 150px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; position: relative; overflow: hidden; box-shadow: var(--shadow-sm);
  transition: box-shadow .15s var(--ease), transform .15s var(--ease);
}
.stat-box:hover { box-shadow: var(--shadow-md); transform: translateY(-2px); }
.stat-box::before { content: ""; position: absolute; top: 0; left: 0; right: 0; height: 3px; background: linear-gradient(90deg, var(--gold-dark), var(--gold)); }
.stat-value { font-family: "Fraunces", serif; font-size: 2rem; font-weight: 700; }
.stat-label { color: var(--text-dim); font-size: .8rem; margin-top: 2px; }
h3 { font-size: .95rem; font-weight: 700; letter-spacing: -.01em; margin: 26px 0 10px; }
canvas { display: block; width: 100%; height: 180px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 22px; box-shadow: var(--shadow-sm); }
.date-lookup { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.date-lookup input[type=date] { padding: 9px 11px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2); color: var(--text); }
#lookup-result { color: var(--text-dim); font-size: .88rem; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow-lg);
  padding: 12px 20px; border-radius: var(--radius); font-size: .87rem; z-index: 200;
  animation: toastin .25s var(--ease);
}
@keyframes toastin { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }

@media (max-width: 640px) {
  .timer-display { font-size: 2.3rem; }
  .header-right { gap: 8px; }
  .modal { max-height: 96vh; }
  main { padding: 16px 12px 50px; }
  .app-header { padding: 10px 14px; }
}
