/* ------------------------------------------------------------------ tokens */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f1f4;
  --border: #e3e5ea;
  --text: #1c1f24;
  --muted: #6b7280;
  --accent: #5b5bd6;
  --accent-contrast: #ffffff;
  --accent-soft: #ececfb;
  --danger: #d1394b;
  --danger-soft: #fdecee;
  --pending: #6b7280;
  --pending-soft: #eef0f3;
  --waiting: #b26a00;
  --waiting-soft: #fff4dc;
  --progress: #2563eb;
  --progress-soft: #e3edff;
  --done: #15803d;
  --done-soft: #e3f6e9;
  --planned: #7c3aed;
  --planned-soft: #f1e9ff;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .08);
  --shadow-lg: 0 20px 50px rgba(16, 24, 40, .22);
  --sidebar-w: 264px;
  --tabbar-h: 64px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f25;
    --surface-2: #252932;
    --border: #2f343e;
    --text: #e7e9ee;
    --muted: #9198a5;
    --accent: #8b8bf0;
    --accent-contrast: #121318;
    --accent-soft: #2a2b4a;
    --danger: #f2687a;
    --danger-soft: #3a1f25;
    --pending: #a3aab6;
    --pending-soft: #2a2e37;
    --waiting: #f0b24d;
    --waiting-soft: #3a2f1a;
    --progress: #7aa7ff;
    --progress-soft: #1f2d4a;
    --done: #5fd38d;
    --done-soft: #1b3326;
    --planned: #b99af7;
    --planned-soft: #2e2346;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    --shadow-lg: 0 20px 50px rgba(0, 0, 0, .55);
    color-scheme: dark;
  }
}

/* ------------------------------------------------------------------ base */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.25; margin: 0; }
h1 { font-size: 1.75rem; font-weight: 700; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; font-weight: 650; }
code { font: .85em ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--surface-2); padding: .15em .4em; border-radius: 5px; word-break: break-all; }
kbd { font: 11px ui-monospace, monospace; border: 1px solid currentColor; border-radius: 4px; padding: 0 .35em; opacity: .6; margin-left: auto; }
.icon { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.muted { color: var(--muted); font-size: .9em; }
.center { text-align: center; }
.stack { display: flex; flex-direction: column; gap: 14px; }

/* ------------------------------------------------------------------ layout */
.app { display: flex; min-height: 100vh; min-height: 100dvh; }

.sidebar {
  position: sticky; top: 0; height: 100vh; height: 100dvh;
  width: var(--sidebar-w); flex: none;
  display: flex; flex-direction: column; gap: 16px;
  padding: 20px 14px;
  background: var(--surface); border-right: 1px solid var(--border);
  overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 750; font-size: 1.15rem; color: var(--text); padding: 0 8px; }
.brand:hover { text-decoration: none; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 8px; background: var(--accent);
  -webkit-mask: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath d='M9 16.5l4.5 4.5L23 11.5' stroke='white' stroke-width='3.2' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-size: cover;
}
.side-nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.side-nav a, .sidebar-foot a {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px; border-radius: var(--radius-sm);
  color: var(--text); font-weight: 500;
}
.side-nav a:hover, .sidebar-foot a:hover { background: var(--surface-2); text-decoration: none; }
.side-nav a.active, .sidebar-foot a.active { background: var(--accent-soft); color: var(--accent); }
.side-nav .count { margin-left: auto; }
.nav-area { margin: 14px 10px 4px; font-size: .75rem; font-weight: 650; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.nav-project { font-weight: 450 !important; }
.nav-empty { padding: 4px 10px; color: var(--muted); font-size: .85rem; }
.nav-manage { margin-top: 10px; color: var(--muted) !important; font-size: .88rem; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); opacity: .55; flex: none; }
.sidebar-foot { border-top: 1px solid var(--border); padding-top: 12px; display: flex; flex-direction: column; gap: 2px; }
.sidebar-foot form { padding: 4px 10px; }

.main {
  flex: 1; min-width: 0;
  max-width: 820px;
  margin: 0 auto;
  padding: 32px 28px 80px;
}

.tabbar { display: none; }
.mobile-only { display: none; }

/* ------------------------------------------------------------------ page parts */
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.subtitle { margin: 4px 0 0; color: var(--muted); }
.eyebrow { margin: 0 0 2px; font-size: .8rem; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; }
.eyebrow a { color: var(--muted); }
.back { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-weight: 500; }

.section-title { display: flex; align-items: center; gap: 8px; font-size: .8rem; font-weight: 650; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 8px; }
summary.section-title { cursor: pointer; list-style: none; }
summary.section-title::-webkit-details-marker { display: none; }
summary.section-title::before { content: "▸"; font-size: .9em; transition: transform .15s; }
details[open] > summary.section-title::before { transform: rotate(90deg); }
.section-title .hint { margin-left: auto; text-transform: none; letter-spacing: 0; font-weight: 450; }
.count { font-size: .78rem; font-weight: 600; color: var(--muted); background: var(--surface-2); border-radius: 99px; padding: 0 8px; line-height: 1.6; text-transform: none; letter-spacing: 0; }
.badge { font-size: .75rem; vertical-align: middle; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); font-weight: 600; }

.group { margin: 26px 0; }
.empty { color: var(--muted); padding: 28px 16px; text-align: center; background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; box-shadow: var(--shadow); }
.card > h2 { margin-bottom: 10px; }
.card p { margin: 0 0 12px; }

.flash { margin-bottom: 16px; display: grid; gap: 8px; }
.flash > div, .flash-error { padding: 10px 14px; border-radius: var(--radius-sm); }
.flash-success, .flash-info { background: var(--done-soft); color: var(--done); }
.flash-error { background: var(--danger-soft); color: var(--danger); }
.error { color: var(--danger); }

/* ------------------------------------------------------------------ controls */
.input {
  width: 100%; min-height: 42px;
  font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 8px 12px;
  transition: border-color .15s, box-shadow .15s;
}
.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea.input { resize: vertical; min-height: 80px; line-height: 1.5; }
select.input { appearance: none; padding-right: 32px; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 17px) 55%, calc(100% - 12px) 55%; background-size: 5px 5px; background-repeat: no-repeat; }
.input-lg { font-size: 1.1rem; min-height: 48px; }
.input-title { font-size: 1.5rem; font-weight: 650; border-color: transparent; background: transparent; padding-left: 0; }
.input-title:focus { padding-left: 12px; background: var(--surface); }
.field { display: flex; flex-direction: column; gap: 5px; flex: 1; min-width: 0; }
.field > span { font-size: .8rem; font-weight: 600; color: var(--muted); }
.field-row { display: flex; gap: 12px; flex-wrap: wrap; }
.field-row .field { min-width: 150px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 42px; padding: 8px 16px;
  font: inherit; font-weight: 600; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  cursor: pointer; white-space: nowrap; text-decoration: none !important;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-danger { color: var(--danger); border-color: var(--danger-soft); background: var(--danger-soft); }
.btn-danger:hover { background: var(--danger-soft); filter: brightness(.97); }
.btn-block { width: 100%; }
.link { background: none; border: 0; padding: 0; font: inherit; color: var(--muted); cursor: pointer; }
.link:hover { color: var(--text); }
.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px; flex: none;
  border-radius: var(--radius-sm); border: 0; background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.subtle { width: 34px; height: 34px; opacity: .55; }
.icon-btn.subtle:hover { opacity: 1; }

.inline-add { display: flex; gap: 8px; margin-bottom: 18px; }
.inline-add .input { flex: 1; }
.inline-add.compact { margin: 10px 0 0; }

/* dropdown menu built on <details> */
.menu { position: relative; }
.menu > summary { list-style: none; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-panel {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 20;
  width: min(300px, 86vw);
  display: flex; flex-direction: column; gap: 10px;
  padding: 14px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-lg);
}

/* ------------------------------------------------------------------ tasks */
.tasks { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.task {
  display: flex; align-items: center; gap: 6px;
  min-height: 56px; padding: 6px 10px 6px 8px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.task:last-child { border-bottom: 0; }
.task:hover { background: color-mix(in srgb, var(--surface-2) 50%, var(--surface)); }
.task.is-done .task-name { color: var(--muted); text-decoration: line-through; }

.handle { display: grid; place-items: center; width: 28px; height: 44px; flex: none; cursor: grab; color: var(--muted); touch-action: none; opacity: .6; }
.handle svg { width: 18px; height: 18px; fill: currentColor; }
.handle:active { cursor: grabbing; }
.drag-ghost { opacity: .35; background: var(--accent-soft); }
.drag-chosen { box-shadow: var(--shadow-lg); }

.check-form { margin: 0; display: contents; }
.check {
  display: grid; place-items: center; flex: none;
  width: 44px; height: 44px; padding: 0; border: 0; background: transparent; cursor: pointer;
}
.check svg {
  width: 22px; height: 22px; border-radius: 50%;
  border: 2px solid var(--border); padding: 2px;
  fill: none; stroke: transparent; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round;
  transition: all .15s;
}
.check:hover svg { border-color: var(--done); stroke: var(--done); }
.is-done > .check-form .check svg, .is-done > .check svg { background: var(--done); border-color: var(--done); stroke: #fff; }
.check.small { width: 36px; height: 36px; }
.check.small svg { width: 20px; height: 20px; }
.status-in_progress > .check-form .check svg { border-color: var(--progress); }

.task-main { flex: 1; min-width: 0; display: flex; flex-direction: column; padding: 6px 4px; color: var(--text); }
.task-main:hover { text-decoration: none; }
.task-name { overflow-wrap: anywhere; font-weight: 500; }
.task-meta { display: flex; flex-wrap: wrap; gap: 4px 10px; font-size: .8rem; color: var(--muted); }
.task-meta:empty { display: none; }
.meta-due.today { color: var(--progress); font-weight: 600; }
.meta-due.overdue { color: var(--danger); font-weight: 600; }
.meta-sub::before { content: "☑ "; }

.status-pill {
  flex: none; appearance: none; cursor: pointer;
  font: 600 .75rem/1 var(--font); letter-spacing: .01em;
  padding: 7px 10px; min-height: 30px; border-radius: 99px; border: 0;
  text-align: center;
  color: var(--pending); background: var(--pending-soft);
}
.status-pill:focus { outline: 2px solid var(--accent); outline-offset: 2px; }
.status-waiting > .status-pill { color: var(--waiting); background: var(--waiting-soft); }
.status-planned > .status-pill { color: var(--planned); background: var(--planned-soft); }
.status-planned > .check-form .check svg { border-color: var(--planned); }
.status-in_progress > .status-pill { color: var(--progress); background: var(--progress-soft); }
.status-done > .status-pill { color: var(--done); background: var(--done-soft); }

/* ------------------------------------------------------------------ inline editor */
.task-editor {
  padding: 16px; background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: inset 3px 0 0 var(--accent);
}
.task-editor:last-child { border-bottom: 0; }
.task-editor .input-title { font-size: 1.2rem; }
.task-editor .subtasks { margin: 18px 0 0; }
.task-editor .inline-add { margin-bottom: 0; }
.editor-actions { display: flex; gap: 10px; }
.editor-actions .editor-delete { margin-left: auto; }

/* ------------------------------------------------------------------ date picker */
.flatpickr-input[readonly] { cursor: text; }
.flatpickr-calendar { font-family: var(--font); border-radius: var(--radius); box-shadow: var(--shadow-lg); }
.flatpickr-day.selected, .flatpickr-day.selected:hover { background: var(--accent); border-color: var(--accent); }
.flatpickr-day.today { border-color: var(--accent); }
.flatpickr-wrapper { display: block; width: 100%; }
@media (prefers-color-scheme: dark) {
  .flatpickr-calendar, .flatpickr-months .flatpickr-month, .flatpickr-weekdays, span.flatpickr-weekday,
  .flatpickr-current-month .flatpickr-monthDropdown-months { background: var(--surface); color: var(--text); fill: var(--text); }
  .flatpickr-calendar.arrowTop::after, .flatpickr-calendar.arrowBottom::after { border-bottom-color: var(--surface); border-top-color: var(--surface); }
  .flatpickr-day { color: var(--text); }
  .flatpickr-day:hover { background: var(--surface-2); border-color: var(--surface-2); }
  .flatpickr-day.prevMonthDay, .flatpickr-day.nextMonthDay, .flatpickr-day.flatpickr-disabled { color: var(--muted); }
  .flatpickr-months .flatpickr-prev-month svg, .flatpickr-months .flatpickr-next-month svg { fill: var(--text); }
  .numInputWrapper span.arrowUp::after { border-bottom-color: var(--text); }
  .numInputWrapper span.arrowDown::after { border-top-color: var(--text); }
}

/* ------------------------------------------------------------------ task detail */
.task-form { margin-bottom: 28px; }
.form-actions { display: flex; gap: 10px; }
.button-row { display: flex; flex-wrap: wrap; gap: 10px; }
.button-row form { margin: 0; }
.subtasks { margin-bottom: 28px; }
.subtask-list { list-style: none; padding: 0; margin: 0 0 10px; }
.subtask { display: flex; align-items: center; gap: 4px; border-bottom: 1px solid var(--border); min-height: 44px; }
.subtask-text { flex: 1; overflow-wrap: anywhere; }
.subtask.is-done .subtask-text { color: var(--muted); text-decoration: line-through; }
.subtask.is-done .check svg { background: var(--done); border-color: var(--done); stroke: #fff; }
.detail-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding-top: 18px; border-top: 1px solid var(--border); }

/* ------------------------------------------------------------------ projects page */
.area-head { display: flex; align-items: center; justify-content: space-between; }
.project-list { list-style: none; margin: 6px 0 0; padding: 0; }
.project-list a { display: flex; align-items: center; gap: 10px; padding: 10px 6px; border-bottom: 1px solid var(--border); color: var(--text); min-height: 44px; }
.project-list a:hover { background: var(--surface-2); text-decoration: none; }
.project-list .count { margin-left: auto; }

/* ------------------------------------------------------------------ settings */
.token-new { background: var(--accent-soft); border-radius: var(--radius-sm); padding: 14px; margin-bottom: 16px; }
.copy-row { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.copy-row code { flex: 1; padding: 8px 10px; background: var(--surface); }
.token-list { list-style: none; padding: 0; margin: 0; }
.token-list li { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-top: 1px solid var(--border); }

/* ------------------------------------------------------------------ dialog */
.dialog {
  width: min(560px, calc(100vw - 24px));
  padding: 0; border: 1px solid var(--border); border-radius: 16px;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow-lg);
}
.dialog::backdrop { background: rgba(10, 12, 16, .45); backdrop-filter: blur(2px); }
.dialog[open] { animation: pop .16s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }
.quick-add-form { display: flex; flex-direction: column; gap: 14px; padding: 18px; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; }

/* ------------------------------------------------------------------ toasts */
#toasts { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 100; display: grid; gap: 8px; pointer-events: none; }
.toast { padding: 10px 16px; border-radius: 99px; background: var(--text); color: var(--bg); font-weight: 550; box-shadow: var(--shadow-lg); animation: pop .16s ease-out; transition: opacity .35s; text-align: center; }
.toast-error { background: var(--danger); color: #fff; }
.toast.leaving { opacity: 0; }

/* ------------------------------------------------------------------ auth */
.auth-main { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 28px; box-shadow: var(--shadow); }
.auth-card .brand { padding: 0; }

/* ------------------------------------------------------------------ mobile */
@media (max-width: 760px) {
  body { font-size: 16px; } /* 16px avoids iOS zoom-on-focus */
  .sidebar { display: none; }
  .main { padding: 18px 16px calc(var(--tabbar-h) + 32px + env(safe-area-inset-bottom)); max-width: none; }
  h1 { font-size: 1.55rem; }
  .mobile-only { display: inline-flex; }

  .tabbar {
    display: flex; position: fixed; inset: auto 0 0 0; z-index: 50;
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    backdrop-filter: saturate(1.6) blur(12px);
    border-top: 1px solid var(--border);
  }
  .tabbar > a {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    color: var(--muted); font-size: .7rem; font-weight: 600;
  }
  .tabbar > a:hover { text-decoration: none; }
  .tabbar > a.active { color: var(--accent); }
  .tab-add {
    align-self: center; display: grid; place-items: center;
    width: 52px; height: 52px; margin: 0 6px; border-radius: 50%; border: 0;
    background: var(--accent); color: var(--accent-contrast); box-shadow: var(--shadow-lg); cursor: pointer;
  }
  .tab-add .icon { width: 26px; height: 26px; }

  .task { padding-right: 8px; }
  .status-pill { max-width: 104px; }
  .handle { width: 32px; }
  #toasts { bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom)); }

  .dialog { margin: auto auto 0; width: 100vw; max-width: 100vw; border-radius: 18px 18px 0 0; border-bottom: 0; padding-bottom: env(safe-area-inset-bottom); }
  .field-row .field { min-width: calc(50% - 6px); }
  .detail-foot { flex-direction: column-reverse; align-items: stretch; }
  .task-editor { padding: 14px 12px; }
  kbd { display: none; }
}
