:root {
  --bg: #f7f7f9;
  --surface: #ffffff;
  --surface-2: #f1f1f5;
  --border: #e6e6ec;
  --border-strong: #d6d6de;
  --text: #16161d;
  --text-2: #5c5c6b;
  --text-3: #9a9aa8;
  --accent: #5b5bf0;
  --accent-soft: rgba(91, 91, 240, 0.12);
  --today: #5b5bf0;
  --danger: #e5484d;
  --shadow: 0 1px 2px rgba(16, 16, 30, 0.04), 0 8px 24px rgba(16, 16, 30, 0.08);
  --shadow-lg: 0 24px 64px rgba(16, 16, 30, 0.18);
  --radius: 12px;
  --hour: 52px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #0e0e12;
  --surface: #16161c;
  --surface-2: #1e1e26;
  --border: #26262f;
  --border-strong: #33333e;
  --text: #ececf1;
  --text-2: #a3a3b2;
  --text-3: #6b6b7a;
  --accent: #7c7cff;
  --accent-soft: rgba(124, 124, 255, 0.16);
  --today: #7c7cff;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 24px 64px rgba(0, 0, 0, 0.6);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  transition: background 0.2s, color 0.2s;
}
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 272px 1fr; grid-template-rows: minmax(0, 1fr); height: 100vh; height: 100dvh; }
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  overflow-y: auto;
}
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; letter-spacing: -0.01em; padding: 0 4px; }
.brand-mark {
  width: 24px; height: 24px; border-radius: 7px;
  background: linear-gradient(135deg, #7c7cff, #b36bff 60%, #ff7ab6);
  box-shadow: 0 4px 12px rgba(124, 124, 255, 0.4);
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 36px; padding: 0 14px; border-radius: 10px;
  border: 1px solid transparent; background: none; font-weight: 500;
  transition: background 0.15s, border-color 0.15s, transform 0.1s, box-shadow 0.15s;
}
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--accent); color: #fff; box-shadow: 0 4px 14px var(--accent-soft); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-ghost { border-color: var(--border); background: var(--surface); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: rgba(229, 72, 77, 0.1); }
.btn-new { height: 42px; width: 100%; font-weight: 600; }

.icon-btn {
  width: 34px; height: 34px; border-radius: 9px; border: none; background: none;
  display: inline-grid; place-items: center; color: var(--text-2);
  transition: background 0.15s, color 0.15s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.sm { width: 26px; height: 26px; border-radius: 7px; }

/* ---------- mini calendar ---------- */
.mini-head { display: flex; align-items: center; justify-content: space-between; padding: 0 4px 8px; font-weight: 600; }
.mini-nav { display: flex; }
.mini-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; text-align: center; }
.mini-grid .dow { font-size: 11px; color: var(--text-3); font-weight: 500; padding: 4px 0; }
.mini-grid button {
  aspect-ratio: 1; border: none; background: none; border-radius: 50%;
  font-size: 12px; color: var(--text); position: relative;
}
.mini-grid button:hover { background: var(--surface-2); }
.mini-grid button.out { color: var(--text-3); }
.mini-grid button.in-range { background: var(--accent-soft); border-radius: 6px; }
.mini-grid button.today { color: var(--accent); font-weight: 700; }
.mini-grid button.selected { background: var(--accent); color: #fff; font-weight: 600; border-radius: 50%; }
.mini-grid button.has::after {
  content: ""; position: absolute; bottom: 3px; left: 50%; transform: translateX(-50%);
  width: 3px; height: 3px; border-radius: 50%; background: currentColor; opacity: 0.5;
}

/* ---------- sidebar sections ---------- */
.side-section h3 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-3); margin: 0 4px 8px; font-weight: 600; }
.cal-list, .upcoming { list-style: none; margin: 0; padding: 0; }
.cal-list label {
  display: flex; align-items: center; gap: 10px; padding: 7px 6px; border-radius: 8px; cursor: pointer;
}
.cal-list label:hover { background: var(--surface-2); }
.cal-list input { display: none; }
.cal-check {
  width: 16px; height: 16px; border-radius: 5px; border: 2px solid var(--c);
  display: grid; place-items: center; transition: background 0.15s;
}
.cal-list input:checked + .cal-check { background: var(--c); }
.cal-check svg { opacity: 0; }
.cal-list input:checked + .cal-check svg { opacity: 1; }

.upcoming li {
  display: flex; gap: 10px; padding: 8px 6px; border-radius: 8px; cursor: pointer;
}
.upcoming li:hover { background: var(--surface-2); }
.upcoming .bar { width: 3px; border-radius: 2px; background: var(--c); flex-shrink: 0; }
.upcoming .u-title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.upcoming .u-meta { font-size: 12px; color: var(--text-2); margin-top: 2px; }
.upcoming .empty { color: var(--text-3); font-size: 13px; padding: 4px 6px; cursor: default; }
.upcoming .empty:hover { background: none; }

/* ---------- toolbar ---------- */
.toolbar {
  display: flex; align-items: center; gap: 10px; padding: 14px 20px;
  border-bottom: 1px solid var(--border); background: var(--surface);
}
.menu-btn { display: none; }
.nav { display: flex; }
.title { font-size: 20px; font-weight: 600; letter-spacing: -0.02em; margin: 0 0 0 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.spacer { flex: 1; }

.seg {
  position: relative; display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px;
}
.seg button {
  position: relative; z-index: 1; border: none; background: none; padding: 6px 14px; border-radius: 8px;
  color: var(--text-2); font-weight: 500; transition: color 0.2s;
}
.seg button.active { color: var(--text); }
.seg-pill {
  position: absolute; top: 3px; bottom: 3px; left: 0; border-radius: 8px;
  background: var(--surface); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
  transition: transform 0.25s cubic-bezier(0.3, 0.7, 0.2, 1), width 0.25s;
}

:root:not([data-theme="dark"]) .i-moon, :root[data-theme="dark"] .i-sun { display: none; }

/* ---------- view container ---------- */
.view { flex: 1; min-height: 0; display: flex; flex-direction: column; padding: 16px 20px 20px; }
.view.fade { animation: fade 0.22s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ---------- month ---------- */
.month {
  flex: 1; display: grid; grid-template-rows: auto 1fr; min-height: 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow);
}
.month-dow { display: grid; grid-template-columns: repeat(7, 1fr); border-bottom: 1px solid var(--border); }
.month-dow div { padding: 10px 12px; font-size: 12px; font-weight: 600; color: var(--text-3); text-transform: uppercase; letter-spacing: 0.06em; }
.month-grid { display: grid; grid-template-columns: repeat(7, 1fr); grid-template-rows: repeat(6, minmax(0, 1fr)); min-height: 0; }
.cell {
  border-right: 1px solid var(--border); border-bottom: 1px solid var(--border);
  padding: 6px; display: flex; flex-direction: column; gap: 3px; min-height: 0; overflow: hidden;
  cursor: pointer; transition: background 0.12s;
}
.cell:nth-child(7n) { border-right: none; }
.cell:nth-last-child(-n + 7) { border-bottom: none; }
.cell:hover { background: var(--surface-2); }
.cell.out { background: color-mix(in srgb, var(--surface-2) 50%, transparent); }
.cell.out .daynum { color: var(--text-3); }
.cell.weekend:not(.out) { background: color-mix(in srgb, var(--surface-2) 25%, transparent); }
.daynum {
  align-self: flex-start; font-size: 13px; font-weight: 500; width: 26px; height: 26px;
  display: grid; place-items: center; border-radius: 50%; margin-bottom: 1px;
}
.cell.today .daynum { background: var(--today); color: #fff; font-weight: 700; }

.pill {
  display: flex; align-items: center; gap: 6px; font-size: 12px; padding: 2px 6px; border-radius: 6px;
  white-space: nowrap; overflow: hidden; flex-shrink: 0; transition: transform 0.1s, filter 0.1s;
}
.pill:hover { filter: brightness(0.96); }
.pill.allday { background: var(--c); color: #fff; font-weight: 500; }
.pill.timed .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--c); flex-shrink: 0; }
.pill.timed .t { color: var(--text-2); font-variant-numeric: tabular-nums; }
.pill.timed:hover { background: var(--surface); }
.pill .name { overflow: hidden; text-overflow: ellipsis; }
.more { font-size: 12px; color: var(--text-2); font-weight: 600; padding: 1px 6px; border-radius: 6px; align-self: flex-start; }
.more:hover { background: var(--border); color: var(--text); }

/* ---------- week / day ---------- */
.tgrid {
  flex: 1; display: flex; flex-direction: column; min-height: 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow);
}
.tg-head, .tg-allday, .tg-body-inner { display: grid; grid-template-columns: 60px repeat(var(--cols), 1fr); }
.tg-head { border-bottom: 1px solid var(--border); }
.tg-head, .tg-allday, .tg-body { overflow-y: hidden; scrollbar-gutter: stable; scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
.tg-body { overflow-y: auto; }
.tg-head .colhead { padding: 10px 8px; text-align: center; cursor: pointer; border-left: 1px solid var(--border); }
.tg-head .colhead:hover { background: var(--surface-2); }
.colhead .dw { font-size: 11px; font-weight: 600; color: var(--text-3); text-transform: uppercase; letter-spacing: 0.06em; }
.colhead .dn { font-size: 22px; font-weight: 600; margin-top: 2px; width: 38px; height: 38px; display: inline-grid; place-items: center; border-radius: 50%; letter-spacing: -0.02em; }
.colhead.today .dw { color: var(--accent); }
.colhead.today .dn { background: var(--today); color: #fff; }
.tg-allday { border-bottom: 1px solid var(--border); min-height: 30px; }
.tg-allday .lbl { font-size: 10px; color: var(--text-3); padding: 8px 6px; text-align: right; text-transform: uppercase; letter-spacing: 0.05em; }
.tg-allday .adcol { border-left: 1px solid var(--border); padding: 3px; display: flex; flex-direction: column; gap: 2px; cursor: pointer; }
.tg-body { flex: 1; overflow-y: auto; position: relative; scroll-behavior: smooth; }
.tg-body-inner { position: relative; height: calc(var(--hour) * 24); }
.hours { position: relative; }
.hours span {
  position: absolute; right: 8px; transform: translateY(-50%); font-size: 11px; color: var(--text-3);
  font-variant-numeric: tabular-nums;
}
.daycol {
  position: relative; border-left: 1px solid var(--border);
  background-image: repeating-linear-gradient(to bottom, var(--border) 0 1px, transparent 1px var(--hour));
}
.daycol.today { background-color: color-mix(in srgb, var(--accent-soft) 35%, transparent); }
.ev {
  position: absolute; border-radius: 8px; padding: 4px 7px; overflow: hidden; cursor: pointer;
  background: color-mix(in srgb, var(--c) 16%, var(--surface));
  border-left: 3px solid var(--c); font-size: 12px; line-height: 1.3;
  box-shadow: 0 0 0 1px var(--surface);
  transition: box-shadow 0.15s, transform 0.1s;
}
.ev:hover { box-shadow: 0 0 0 1px var(--surface), 0 6px 16px rgba(0, 0, 0, 0.15); z-index: 5; }
.ev .ev-title { font-weight: 600; color: color-mix(in srgb, var(--c) 55%, var(--text)); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ev .ev-time { color: var(--text-2); font-size: 11px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.now-line { position: absolute; left: 0; right: 0; height: 2px; background: var(--danger); z-index: 6; pointer-events: none; }
.now-line::before { content: ""; position: absolute; left: -5px; top: -4px; width: 10px; height: 10px; border-radius: 50%; background: var(--danger); }
.ghost {
  position: absolute; left: 2px; right: 2px; border-radius: 8px; background: var(--accent-soft);
  border: 1.5px dashed var(--accent); pointer-events: none; z-index: 4;
}

/* ---------- drag & drop ---------- */
.ev, .pill { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.ev { cursor: grab; }
.ev-resize { position: absolute; left: 0; right: 0; bottom: 0; height: 8px; cursor: ns-resize; touch-action: none; }
.ev.lifted, .pill.lifted {
  z-index: 20; transform: scale(1.04);
  box-shadow: 0 0 0 2px var(--accent), 0 14px 32px rgba(0, 0, 0, 0.25);
}
.ev-resize::after {
  content: ""; position: absolute; left: 50%; bottom: 2px; width: 18px; height: 3px; margin-left: -9px;
  border-radius: 2px; background: var(--c); opacity: 0; transition: opacity 0.15s;
}
.ev:hover .ev-resize::after, .ev.dragging .ev-resize::after { opacity: 0.6; }
.ev.dragging {
  z-index: 20; opacity: 0.94; transition: none;
  box-shadow: 0 0 0 1px var(--surface), 0 14px 32px rgba(0, 0, 0, 0.25);
}
.pill.dragging { z-index: 20; background: var(--surface); box-shadow: 0 6px 18px rgba(0, 0, 0, 0.2); }
.pill.allday.dragging { background: var(--c); }
.drop-target { background: var(--accent-soft) !important; box-shadow: inset 0 0 0 2px var(--accent); }
body.grabbing, body.grabbing * { cursor: grabbing !important; }
body.resizing, body.resizing * { cursor: ns-resize !important; }

.toast {
  position: fixed; left: 50%; bottom: 24px; z-index: 100; max-width: calc(100vw - 32px);
  display: flex; align-items: center; gap: 14px; padding: 8px 8px 8px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg); box-shadow: var(--shadow-lg); font-weight: 500;
  opacity: 0; pointer-events: none; transform: translate(-50%, 16px);
  transition: opacity 0.2s, transform 0.25s cubic-bezier(0.3, 0.7, 0.2, 1);
}
.toast.show { opacity: 1; pointer-events: auto; transform: translate(-50%, 0); }
.toast span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.toast button {
  border: none; border-radius: 8px; padding: 6px 12px; font-weight: 600; flex-shrink: 0;
  background: color-mix(in srgb, var(--bg) 18%, transparent);
}
.toast button:hover { background: color-mix(in srgb, var(--bg) 28%, transparent); }

/* ---------- sync ---------- */
[hidden] { display: none !important; }
.sync-btn { position: relative; }
.sync-dot {
  position: absolute; right: 5px; bottom: 6px; width: 9px; height: 9px; border-radius: 50%;
  border: 2px solid var(--surface); background: var(--text-3);
}
.sync-btn[data-status="off"] .sync-dot { display: none; }
.sync-btn[data-status="ok"] .sync-dot { background: #20b26b; }
.sync-btn[data-status="syncing"] .sync-dot { background: var(--accent); animation: pulse 1s ease-in-out infinite; }
.sync-btn[data-status="offline"] .sync-dot { background: #f2994a; }
.sync-btn[data-status="unauthorized"] .sync-dot, .sync-btn[data-status="error"] .sync-dot { background: var(--danger); }
@keyframes pulse { 50% { opacity: 0.35; } }

.modal-title { flex: 1; margin: 0; font-size: 22px; font-weight: 600; letter-spacing: -0.02em; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.sync-state {
  margin: 0; padding: 10px 12px; border-radius: 10px; background: var(--surface-2); color: var(--text-2);
  display: flex; align-items: center; gap: 10px;
}
.sync-state::before { content: ""; width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; background: var(--text-3); }
.sync-state[data-status="ok"]::before { background: #20b26b; }
.sync-state[data-status="syncing"]::before { background: var(--accent); }
.sync-state[data-status="offline"]::before { background: #f2994a; }
.sync-state[data-status="unauthorized"]::before, .sync-state[data-status="error"]::before { background: var(--danger); }
.hint { margin: 0; font-size: 13px; color: var(--text-2); line-height: 1.45; }
.sync-error { margin: 0; font-size: 13px; color: var(--danger); }
.sync-error:empty { display: none; }

/* ---------- modal ---------- */
.modal {
  border: 1px solid var(--border); border-radius: 18px; padding: 0; width: min(460px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: var(--shadow-lg);
}
.modal[open] { animation: pop 0.2s cubic-bezier(0.3, 0.7, 0.2, 1.1); }
.modal::backdrop { background: rgba(10, 10, 20, 0.35); backdrop-filter: blur(4px); }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(0.97); } to { opacity: 1; transform: none; } }
.modal form { padding: 20px; display: flex; flex-direction: column; gap: 16px; }
.modal-head { display: flex; gap: 8px; align-items: center; }
.title-input {
  flex: 1; border: none; background: none; font-size: 22px; font-weight: 600; outline: none; letter-spacing: -0.02em;
  padding: 4px 0; border-bottom: 2px solid var(--border); transition: border-color 0.15s;
}
.title-input:focus { border-color: var(--accent); }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 12px; font-weight: 600; color: var(--text-2); }
.field input, .field textarea {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 10px; padding: 9px 11px; outline: none;
  transition: border-color 0.15s, box-shadow 0.15s; resize: vertical;
}
.field input:focus, .field textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field-row { display: flex; gap: 12px; align-items: flex-end; }
.grow { flex: 1; }
.times.hidden { display: none; }

.toggle { display: flex; align-items: center; gap: 8px; cursor: pointer; height: 40px; font-weight: 500; color: var(--text-2); user-select: none; }
.toggle input { display: none; }
.track { width: 36px; height: 21px; border-radius: 11px; background: var(--border-strong); position: relative; transition: background 0.2s; }
.thumb { position: absolute; top: 2px; left: 2px; width: 17px; height: 17px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.2); transition: transform 0.2s; }
.toggle input:checked + .track { background: var(--accent); }
.toggle input:checked + .track .thumb { transform: translateX(15px); }

.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch {
  display: flex; align-items: center; gap: 7px; padding: 6px 11px 6px 8px; border-radius: 20px;
  border: 1px solid var(--border); cursor: pointer; font-size: 13px; transition: all 0.15s;
}
.swatch input { display: none; }
.swatch i { width: 12px; height: 12px; border-radius: 50%; background: var(--c); }
.swatch:has(input:checked) { border-color: var(--c); background: color-mix(in srgb, var(--c) 12%, transparent); font-weight: 600; }
.modal-actions { display: flex; gap: 8px; align-items: center; padding-top: 4px; }

/* ---------- touch ---------- */
body { -webkit-tap-highlight-color: transparent; }
@media (pointer: coarse) {
  /* iOS zooms the page when focusing inputs smaller than 16px. */
  input, textarea, select { font-size: 16px; }
  /* No hover on touch, so keep the resize grip visible and give it a bigger target. */
  .ev-resize { height: 12px; }
  .ev-resize::after { opacity: 0.5; }
}

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: 280px; z-index: 50; transform: translateX(-100%);
    transition: transform 0.25s ease; box-shadow: var(--shadow-lg);
  }
  .sidebar.open { transform: none; }
  .menu-btn { display: inline-grid; }
  .toolbar { flex-wrap: wrap; padding: 12px 16px; }
  .title { font-size: 17px; }
  .view { padding: 12px 16px 16px; }
  .pill.timed .t { display: none; }
}
@media (max-width: 560px) {
  .seg { order: 10; width: 100%; }
  .seg button { flex: 1; }
  .spacer { display: none; }
  .title { flex: 1; }
  .tg-head, .tg-allday, .tg-body-inner { grid-template-columns: 44px repeat(var(--cols), 1fr); }
  .colhead .dn { font-size: 16px; width: 30px; height: 30px; }
  .ev { padding: 3px 4px; }
  .month-dow div { padding: 8px 4px; text-align: center; font-size: 10px; }
  .cell { padding: 3px; }
  .daynum { font-size: 12px; width: 22px; height: 22px; align-self: center; }
  .pill { font-size: 10px; padding: 1px 3px; }
  .pill.timed .dot { display: none; }
}
