:root {
  --bg: #f7f5f0;
  --surface: #ffffff;
  --surface-2: #f1eee7;
  --fg: #1f2328;
  --muted: #80868e;
  --line: #e6e2d9;
  --accent: #3d85c6;
  --accent-fg: #ffffff;
  --danger: #c8443a;
  --ok: #2f9e6e;
  --warn: #d08a1f;
  --shadow: 0 1px 2px rgba(30, 25, 15, 0.05), 0 4px 16px rgba(30, 25, 15, 0.04);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #131518;
    --surface: #1b1e22;
    --surface-2: #23272c;
    --fg: #ebe8e3;
    --muted: #8b9199;
    --line: #2c3036;
    --accent: #6aa8e0;
    --accent-fg: #0d1520;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme='dark'] {
  --bg: #131518;
  --surface: #1b1e22;
  --surface-2: #23272c;
  --fg: #ebe8e3;
  --muted: #8b9199;
  --line: #2c3036;
  --accent: #6aa8e0;
  --accent-fg: #0d1520;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.45 'Inter', -apple-system, 'Segoe UI', system-ui, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 24px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: -0.01em; }
.logo {
  width: 22px; height: 22px; border-radius: 7px;
  background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 60%, #000));
}
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 10px; }
.tab {
  border: 0; background: none; padding: 6px 14px; border-radius: 7px; cursor: pointer; color: var(--muted); font-weight: 500;
}
.tab[aria-selected='true'] { background: var(--surface); color: var(--fg); box-shadow: var(--shadow); }
.top-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }

.btn {
  border: 1px solid var(--line);
  background: var(--surface);
  padding: 7px 14px;
  border-radius: 9px;
  cursor: pointer;
  font-weight: 500;
  white-space: nowrap;
}
.btn:hover { border-color: color-mix(in srgb, var(--fg) 25%, var(--line)); }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: transparent; }
#theme-toggle { display: inline-grid; place-items: center; padding: 7px 9px; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.icon { width: 34px; padding: 7px 0; text-align: center; }
.btn.small { padding: 3px 8px; font-size: 13px; }
.btn.icon.small { width: 26px; padding: 2px 0; }
.btn.wide { width: 100%; padding: 10px; }

.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 220px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 4px; box-shadow: 0 10px 30px rgba(0,0,0,.12);
}
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.menu button:hover { background: var(--surface-2); }
.menu .danger { color: var(--danger); }

main { padding: 20px 24px 60px; max-width: 1600px; margin: 0 auto; }

/* ---------- Week view ---------- */
.week-nav { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px; margin-bottom: 18px; }
.nav-buttons { display: flex; gap: 6px; }
.week-nav h1 { margin: 0; font-size: 26px; letter-spacing: -0.02em; text-align: center; display: flex; gap: 12px; align-items: baseline; }
.week-nav h1 small { font-size: 15px; color: var(--muted); font-weight: 500; letter-spacing: 0; }
.week-nav-end { justify-self: end; display: flex; align-items: center; gap: 12px; }
.year-mini { color: var(--muted); font-size: 13px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.card h2 { margin: 0 0 10px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }

.week-top { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; margin-bottom: 14px; }
.focus-list { margin: 0; padding: 0; list-style: none; counter-reset: f; display: grid; gap: 4px; }
.focus-list li { counter-increment: f; display: flex; align-items: center; gap: 10px; }
.focus-list li::before { content: counter(f); font-weight: 700; color: var(--accent); width: 14px; }
.focus-list input, .notes-card textarea {
  width: 100%; border: 0; background: transparent; outline: none; padding: 5px 0; border-bottom: 1px solid var(--line);
}
.focus-list input:focus, .notes-card textarea:focus { border-color: var(--accent); }
.notes-card textarea { resize: vertical; min-height: 96px; border: 0; background-image: repeating-linear-gradient(transparent 0 27px, var(--line) 27px 28px); line-height: 28px; padding: 0; }

.week-grid { display: grid; grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 10px; }
.day {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 12px 8px;
  min-height: 340px; display: flex; flex-direction: column; box-shadow: var(--shadow); transition: border-color .15s, background .15s;
}
.day.today { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent), var(--shadow); }
.day.past { opacity: 0.78; }
.day.someday { background: var(--surface-2); box-shadow: none; }
.day.drop { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 6%, var(--surface)); }
.day-head { display: flex; justify-content: space-between; align-items: baseline; padding-bottom: 8px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.day-head .wd { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.day-head .dn { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; }
.day.today .wd, .day.today .dn { color: var(--accent); }

/* Fixed blocks from the week template */
.blocks { display: grid; gap: 4px; padding: 6px 0 4px; }
.block {
  --c: var(--accent);
  display: flex; flex-direction: column; gap: 1px; width: 100%; min-width: 0; text-align: left; cursor: pointer;
  border: 0; border-left: 3px solid var(--c); border-radius: 6px; padding: 4px 7px;
  background: color-mix(in srgb, var(--c) 14%, transparent); font-size: 13px;
}
.block .label { font-weight: 600; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.block .time { color: var(--muted); font-size: 12px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.block:hover { background: color-mix(in srgb, var(--c) 22%, transparent); }
.block.skipped { background: none; border-left-style: dashed; opacity: 0.55; }
.block.skipped .label { text-decoration: line-through; font-weight: 500; }

.tasks { list-style: none; margin: 0; padding: 0; flex: 1; }
.task {
  display: flex; align-items: flex-start; gap: 7px; padding: 5px 0; border-bottom: 1px solid var(--line); position: relative; cursor: grab;
}
.task.dragging { opacity: 0.35; }
.task.insert-before::before { content: ''; position: absolute; left: 0; right: 0; top: -2px; height: 2px; background: var(--accent); border-radius: 2px; }
.task .check {
  --c: var(--accent);
  flex: none; width: 16px; height: 16px; margin-top: 2px; border-radius: 50%; border: 1.6px solid color-mix(in srgb, var(--c) 70%, var(--muted)); background: none; padding: 0; cursor: pointer;
}
.task.done .check { background: var(--c); border-color: var(--c); background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'><path d='M4.5 8.2l2.3 2.3 4.7-5' stroke='white' stroke-width='2' fill='none' stroke-linecap='round' stroke-linejoin='round'/></svg>"); }
.task .text { flex: 1; min-width: 0; outline: none; overflow-wrap: anywhere; font-size: 14px; cursor: text; border-radius: 4px; }
.task .text:focus { background: var(--surface-2); box-shadow: 0 0 0 3px var(--surface-2); }
.task.done .text { color: var(--muted); text-decoration: line-through; }
.task .goal-dot {
  --c: var(--line);
  flex: none; width: 10px; height: 10px; border-radius: 50%; border: 1.5px dashed var(--muted); background: none; padding: 0; margin-top: 5px; cursor: pointer; opacity: 0;
}
.task .goal-dot.linked { background: var(--c); border: 0; opacity: 1; }
.task .del { flex: none; border: 0; background: none; color: var(--muted); cursor: pointer; padding: 0 2px; line-height: 1; font-size: 17px; opacity: 0; }
.task:hover .goal-dot, .task:hover .del, .task:focus-within .goal-dot, .task:focus-within .del { opacity: 1; }
.task .del:hover { color: var(--danger); }
@media (hover: none) { .task .goal-dot, .task .del { opacity: 1; } }

.add-task { border: 0; background: none; outline: none; padding: 8px 0 4px; font-size: 14px; width: 100%; color: var(--fg); }
.add-task::placeholder { color: var(--muted); }

/* ---------- Popover ---------- */
.popover {
  position: absolute; z-index: 20; min-width: 220px; max-width: 280px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 6px; box-shadow: 0 12px 30px rgba(0,0,0,.15);
}
.pop-title { font-size: 12px; color: var(--muted); padding: 4px 8px 6px; }
.pop-item { display: flex; gap: 8px; align-items: center; width: 100%; text-align: left; border: 0; background: none; padding: 7px 8px; border-radius: 6px; cursor: pointer; }
.pop-item:hover, .pop-item.active { background: var(--surface-2); }
.pop-item.muted { color: var(--muted); }
.popover .hint { padding: 0 8px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--c, var(--accent)); flex: none; border: 0; padding: 0; }
.swatch.big { width: 16px; height: 16px; cursor: pointer; }

/* ---------- Year view ---------- */
.year-progress { margin-bottom: 16px; }
.yp-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 14px; }
.stat strong { display: block; font-size: 26px; letter-spacing: -0.02em; }
.stat span { color: var(--muted); font-size: 13px; }
.weeks-dots { display: grid; grid-template-columns: repeat(52, minmax(0, 1fr)); gap: 3px; }
.weeks-dots span { height: 10px; border-radius: 3px; background: var(--surface-2); border: 1px solid var(--line); }
.weeks-dots span.past { background: var(--accent); border-color: transparent; opacity: 0.85; }
.weeks-dots span.now { background: var(--accent); border-color: var(--fg); }

.goals { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 12px; margin-bottom: 16px; }
.empty { color: var(--muted); padding: 24px; text-align: center; border: 1.5px dashed var(--line); border-radius: var(--radius); grid-column: 1 / -1; }
.goal { --c: var(--accent); display: flex; flex-direction: column; gap: 12px; }
.goal-head { display: flex; align-items: center; gap: 10px; }
.goal-title { flex: 1; min-width: 0; border: 0; background: none; font-weight: 600; font-size: 16px; outline: none; padding: 2px 0; border-bottom: 1px solid transparent; }
.goal-title:focus { border-color: var(--line); }
.goal .del { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 18px; opacity: 0; }
.goal:hover .del, .goal:focus-within .del { opacity: 1; }
.goal .del:hover { color: var(--danger); }
.badge { font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.ahead { color: var(--ok); background: color-mix(in srgb, var(--ok) 12%, transparent); }
.badge.behind { color: var(--warn); background: color-mix(in srgb, var(--warn) 14%, transparent); }
.badge.late { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }
.badge.reached { color: var(--accent-fg); background: var(--c); }
.bar { position: relative; height: 10px; background: var(--surface-2); border-radius: 99px; }
.bar .fill { height: 100%; border-radius: 99px; background: var(--c); transition: width .3s; min-width: 0; }
.bar .pace { position: absolute; top: -4px; bottom: -4px; width: 2px; margin-left: -1px; background: var(--fg); opacity: .5; border-radius: 2px; }
.goal-foot { display: flex; justify-content: space-between; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.counter { display: flex; align-items: center; gap: 4px; }
.counter strong { color: var(--fg); font-size: 15px; margin-left: 4px; }
.counter .target { width: 64px; border: 1px solid transparent; background: none; padding: 1px 4px; border-radius: 6px; }
.counter .target:hover, .counter .target:focus { border-color: var(--line); outline: none; }
.counter .unit { margin-right: 4px; }
.done-toggle { display: flex; gap: 6px; align-items: center; color: var(--fg); cursor: pointer; }

.add-goal-row { display: grid; grid-template-columns: minmax(0, 1fr) 90px 160px auto; gap: 8px; }
.add-goal-row input { border: 1px solid var(--line); background: var(--bg); border-radius: 9px; padding: 8px 10px; outline: none; min-width: 0; }
.add-goal-row input:focus { border-color: var(--accent); }
.hint { color: var(--muted); font-size: 13px; margin: 8px 0 0; }

/* ---------- Export dialog ---------- */
.export-dialog {
  width: min(1200px, calc(100vw - 32px)); max-height: calc(100vh - 32px); padding: 0; border: 1px solid var(--line); border-radius: 18px;
  background: var(--surface); color: var(--fg); box-shadow: 0 30px 80px rgba(0,0,0,.3);
}
.export-dialog::backdrop { background: rgba(10, 12, 15, 0.5); backdrop-filter: blur(3px); }
.export-layout { display: grid; grid-template-columns: minmax(0, 1fr) 300px; min-height: 0; }
.export-preview { background: var(--surface-2); display: grid; place-items: center; padding: 24px; min-height: 360px; }
#preview-canvas { width: 100%; height: auto; max-height: calc(100vh - 120px); object-fit: contain; border-radius: 8px; box-shadow: 0 10px 40px rgba(0,0,0,.2); }
#preview-canvas.portrait { width: auto; height: calc(100vh - 140px); max-height: 760px; max-width: 100%; }
.export-controls { padding: 18px 20px; display: flex; flex-direction: column; gap: 14px; overflow-y: auto; max-height: calc(100vh - 34px); }
.export-head { display: flex; justify-content: space-between; align-items: center; }
.export-head h2 { margin: 0; font-size: 18px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); border: 0; margin: 0; padding: 0; }
.field > span, .field legend { font-weight: 600; }
.field select, .field input[type='number'] { border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 7px 8px; color: var(--fg); width: 100%; }
.field.two { flex-direction: row; gap: 8px; }
.field.two label { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.field.inline { flex-direction: row; align-items: center; justify-content: space-between; }
.field input[type='color'] { width: 44px; height: 28px; border: 1px solid var(--line); border-radius: 6px; padding: 2px; background: var(--bg); }
.field input[type='range'] { accent-color: var(--accent); }
.field output { font-weight: 400; margin-left: 4px; }
.checks { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.checks legend { margin-bottom: 6px; }
.checks label { color: var(--fg); display: flex; gap: 6px; align-items: center; }
.checks input { accent-color: var(--accent); }
.segmented { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--surface-2); padding: 3px; border-radius: 9px; gap: 2px; }
.segmented button { border: 0; background: none; padding: 5px 0; border-radius: 6px; cursor: pointer; color: var(--muted); font-size: 13px; }
.segmented button[aria-pressed='true'] { background: var(--surface); color: var(--fg); box-shadow: var(--shadow); font-weight: 600; }

/* ---------- Week template dialog ---------- */
.template-dialog {
  width: min(720px, calc(100vw - 32px)); max-height: calc(100vh - 32px); padding: 18px 20px; border: 1px solid var(--line); border-radius: 18px;
  background: var(--surface); color: var(--fg); box-shadow: 0 30px 80px rgba(0,0,0,.3);
}
.template-dialog::backdrop { background: rgba(10, 12, 15, 0.5); backdrop-filter: blur(3px); }
.template-dialog .hint { margin: 6px 0 12px; }
.tpl-summary { display: flex; flex-wrap: wrap; gap: 8px 16px; padding: 10px 12px; background: var(--surface-2); border-radius: 10px; margin-bottom: 8px; font-size: 13px; }
.tpl-total { --c: var(--accent); display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.tpl-total strong { color: var(--fg); }
.tpl-day { padding: 10px 0; border-bottom: 1px solid var(--line); display: grid; gap: 6px; }
.tpl-day:last-child { border-bottom: 0; }
.tpl-day h3 { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.tpl-day .btn.small { justify-self: start; }
.tpl-rows { display: grid; gap: 6px; }
.tpl-row { --c: var(--accent); display: grid; grid-template-columns: 130px minmax(0, 1fr) 112px auto 112px auto; gap: 6px; align-items: center; border-left: 3px solid var(--c); padding-left: 8px; }
.tpl-row select, .tpl-row input { border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 5px 7px; min-width: 0; outline: none; }
.tpl-row select:focus, .tpl-row input:focus { border-color: var(--accent); }
.tpl-dash { color: var(--muted); }
.tpl-row .del { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 18px; padding: 0 4px; }
.tpl-row .del:hover { color: var(--danger); }

/* ---------- Responsive ---------- */
@media (max-width: 1200px) {
  .week-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; padding: 10px 16px; gap: 10px; }
  .top-actions { margin-left: 0; width: 100%; justify-content: flex-end; }
  .brand { flex: 1; }
  main { padding: 16px 16px 48px; }
  .week-nav { grid-template-columns: 1fr; text-align: center; }
  .week-nav h1 { justify-content: center; flex-wrap: wrap; font-size: 22px; }
  .nav-buttons { justify-content: center; }
  .week-nav-end { justify-self: center; }
  .tpl-row { grid-template-columns: 1fr 1fr; }
  .tpl-row .tpl-dash { display: none; }
  .week-top { grid-template-columns: 1fr; }
  .week-grid { grid-template-columns: 1fr; }
  .day { min-height: 0; }
  .yp-stats { grid-template-columns: 1fr 1fr; }
  .weeks-dots { gap: 2px; }
  .goals { grid-template-columns: 1fr; }
  .add-goal-row { grid-template-columns: 1fr 1fr; }
  .add-goal-row input[name='title'] { grid-column: 1 / -1; }
  .add-goal-row button { grid-column: 1 / -1; }
  .export-layout { grid-template-columns: 1fr; }
  .export-preview { min-height: 0; padding: 16px; }
  #preview-canvas.portrait { height: 50vh; }
  .export-controls { max-height: none; }
}
