:root {
  --accent: #3b6fd8;
  --on-accent: #fff;
  --bg: #f4f5f8;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --text: #1d2330;
  --muted: #6b7385;
  --line: #e1e4ea;
  --red: #d33a3a;
  --radius: 12px;
  --hour: 48px;
  --shadow: 0 6px 24px rgba(20, 30, 50, .14);
  --bg-dim: .7;
  color-scheme: light;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151a; --surface: #1c2027; --surface-2: #262b34; --text: #e8eaee; --muted: #9aa2b1; --line: #313744;
    --red: #ff6b6b; --shadow: 0 6px 24px rgba(0, 0, 0, .5); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #12151a; --surface: #1c2027; --surface-2: #262b34; --text: #e8eaee; --muted: #9aa2b1; --line: #313744;
  --red: #ff6b6b; --shadow: 0 6px 24px rgba(0, 0, 0, .5); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  display: flex; flex-direction: column; min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  -webkit-tap-highlight-color: transparent;
}
#bg { position: fixed; inset: 0; z-index: -1; background-size: cover; background-position: center; }
#bg::after { content: ""; position: absolute; inset: 0; background: var(--bg); opacity: var(--bg-dim); }
body.has-bg { background: transparent; }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.spacer { flex: 1; }
[hidden] { display: none !important; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35em;
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: .45em 1em; font-weight: 500; white-space: nowrap; min-height: 36px;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--red); border-color: color-mix(in srgb, var(--red) 40%, transparent); }
.btn.small { padding: .25em .7em; min-height: 28px; font-size: .85rem; }
.btn.block { width: 100%; margin-top: .75rem; }
.icon {
  border: none; background: transparent; border-radius: 50%; width: 38px; height: 38px;
  display: inline-grid; place-items: center; font-size: 1.15rem;
}
.icon:hover { background: var(--surface-2); }
.icon.small { width: 28px; height: 28px; font-size: .85rem; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Topp ---------- */
.topbar {
  display: flex; align-items: center; gap: .75rem; padding: .6rem 1rem; flex-wrap: wrap;
  background: color-mix(in srgb, var(--surface) 92%, transparent); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20; backdrop-filter: blur(8px);
}
.nav { display: flex; align-items: center; gap: .25rem; }
.nav .icon { font-size: 1.6rem; line-height: 1; }
#title { font-size: 1.3rem; margin: 0 .5rem 0 0; font-weight: 700; flex: 1; min-width: 10ch; }
#title small { font-weight: 400; color: var(--muted); font-size: .85rem; margin-left: .4rem; }
.views { display: flex; background: var(--surface-2); border-radius: 999px; padding: 3px; }
.views button { border: none; background: transparent; padding: .4em .95em; border-radius: 999px; font-weight: 500; }
.views button.active { background: var(--accent); color: var(--on-accent); }
.tools { display: flex; gap: .15rem; }

.quick { display: flex; gap: .5rem; align-items: center; padding: .6rem 1rem; flex-wrap: wrap; }
.quick input {
  flex: 1 1 260px; border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: .55em 1.1em; min-height: 40px;
}
.quick input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.quick-preview { order: 3; flex-basis: 100%; font-size: .85rem; color: var(--muted); padding-left: 1.1em; min-height: 0; }
.quick-preview:empty { display: none; }

.view { flex: 1; padding: 0 1rem 5.5rem; min-height: 0; }

/* ---------- Chips ---------- */
.chips { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.chip {
  display: flex; align-items: center; gap: .3em; border: none; text-align: left; width: 100%;
  background: color-mix(in srgb, var(--c) 14%, transparent); color: var(--text);
  border-left: 3px solid var(--c); border-radius: 4px; padding: 1px 5px; font-size: .78rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-height: 20px;
}
.chip.allday { background: var(--c); border-left-color: transparent; }
.chip .t { font-variant-numeric: tabular-nums; opacity: .75; font-size: .72rem; }
.chip .has-img { font-size: .7rem; }
.chip:hover { filter: brightness(.95); }
.more { border: none; background: none; color: var(--muted); font-size: .75rem; text-align: left; padding: 0 5px; }
.hol { font-size: .7rem; color: var(--red); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hol.big { font-size: .95rem; }

/* ---------- År ---------- */
.year-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 1rem; padding-top: .25rem; }
.mini-month { background: var(--surface); border-radius: var(--radius); padding: .5rem .6rem .7rem; border: 1px solid var(--line); }
.mini-head {
  width: 100%; border: none; background: none; text-align: left; font-weight: 700; font-size: 1rem;
  padding: .35rem .3rem; border-radius: 8px; margin-bottom: .2rem;
}
.mini-head:hover { color: var(--accent); }
.mini-head.has-img {
  background-size: cover; background-position: center; height: 64px; display: flex; align-items: flex-end; color: #fff;
  text-shadow: 0 1px 4px rgba(0, 0, 0, .7);
}
.mini-grid { display: grid; grid-template-columns: 1.4em repeat(7, 1fr); gap: 1px; text-align: center; font-size: .8rem; }
.mini-grid .wd { color: var(--muted); font-size: .68rem; font-weight: 600; padding: 2px 0; }
.mini-grid .wk { color: var(--muted); font-size: .62rem; border: none; background: none; padding: 0; align-self: center; }
.mini-grid .wk:hover { color: var(--accent); }
.mini-day {
  position: relative; border: none; background: none; aspect-ratio: 1; border-radius: 6px; padding: 0;
  font-variant-numeric: tabular-nums; display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1;
}
.mini-day:hover { background: var(--surface-2); }
.mini-day.has { background: color-mix(in srgb, var(--c) 16%, transparent); font-weight: 600; }
.mini-day.red { color: var(--red); }
.mini-day.today { background: var(--accent); color: var(--on-accent); font-weight: 700; }
.mini-day .dots { display: flex; gap: 1px; height: 4px; margin-top: 2px; }
.mini-day .dots i { width: 4px; height: 4px; border-radius: 50%; }
.mini-day.today .dots i { background: var(--on-accent) !important; }

/* ---------- Månad ---------- */
.month-banner { height: 140px; border-radius: var(--radius); background-size: cover; background-position: center; margin-bottom: .6rem; }
.month-grid {
  display: grid; grid-template-columns: 2.2em repeat(7, minmax(0, 1fr)); grid-template-rows: auto repeat(var(--weeks), minmax(96px, 1fr));
  background: var(--line); gap: 1px; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden;
  min-height: calc(100dvh - 190px);
}
.mh { background: var(--surface); text-align: center; font-size: .78rem; font-weight: 600; color: var(--muted); padding: .35rem 0; text-transform: capitalize; }
.month-grid .wk { background: var(--surface); border: none; color: var(--muted); font-size: .7rem; padding: .35rem 0; align-self: stretch; }
.month-grid .wk:hover { color: var(--accent); }
.cell { background: var(--surface); padding: 3px 4px 4px; min-width: 0; display: flex; flex-direction: column; gap: 2px; cursor: copy; }
.cell:hover { background: color-mix(in srgb, var(--accent) 5%, var(--surface)); }
.cell.other { background: var(--surface-2); }
.cell.other .daynum { opacity: .45; }
.cell-top { display: flex; align-items: center; gap: .3rem; min-width: 0; }
.daynum {
  border: none; background: none; font-weight: 600; font-size: .85rem; min-width: 26px; height: 26px; border-radius: 50%;
  font-variant-numeric: tabular-nums; padding: 0 4px; flex-shrink: 0;
}
.daynum:hover { background: var(--surface-2); }
.cell.red .daynum { color: var(--red); }
.cell.today .daynum { background: var(--accent); color: var(--on-accent); }

/* ---------- Tidsrutnät ---------- */
.tg { display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; height: calc(100dvh - 160px); min-height: 420px; }
.tg-head, .tg-allday, .tg-body { display: grid; grid-template-columns: 52px repeat(var(--cols), minmax(0, 1fr)); }
.tg-head { border-bottom: 1px solid var(--line); }
.tg-day { border: none; background: none; padding: .4rem 0; display: flex; flex-direction: column; align-items: center; gap: 2px; text-transform: capitalize; }
.tg-day span { font-size: .75rem; color: var(--muted); }
.tg-day b { font-size: 1.15rem; width: 34px; height: 34px; display: grid; place-items: center; border-radius: 50%; }
.tg-day.red b { color: var(--red); }
.tg-day.today b { background: var(--accent); color: var(--on-accent); }
.tg-day:hover b { outline: 2px solid var(--line); }
.tg-gutter { display: flex; align-items: center; justify-content: flex-end; padding-right: 6px; }
.tg-allday { border-bottom: 1px solid var(--line); min-height: 28px; }
.tg-adcell { border-left: 1px solid var(--line); padding: 2px; display: flex; flex-direction: column; gap: 2px; cursor: copy; min-width: 0; }
.tg-scroll { overflow-y: auto; flex: 1; overscroll-behavior: contain; }
.tg-body { position: relative; }
.tg-hours { position: relative; }
.tg-hours div { position: absolute; right: 6px; transform: translateY(-50%); font-size: .7rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.tg-col {
  position: relative; border-left: 1px solid var(--line); cursor: copy;
  background-image: linear-gradient(to bottom, var(--line) 1px, transparent 1px), linear-gradient(to bottom, color-mix(in srgb, var(--line) 50%, transparent) 1px, transparent 1px);
  background-size: 100% var(--hour), 100% calc(var(--hour) / 2);
}
.tg-col.red { background-color: color-mix(in srgb, var(--red) 3%, transparent); }
.tg-ev {
  position: absolute; border: none; border-radius: 6px; padding: 2px 5px; text-align: left; overflow: hidden;
  font-size: .75rem; line-height: 1.2; display: flex; flex-direction: column; box-shadow: 0 1px 2px rgba(0, 0, 0, .15);
  border: 1px solid color-mix(in srgb, var(--surface) 60%, transparent);
}
.tg-ev b { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tg-ev span { opacity: .85; font-size: .7rem; }
.tg-ev:hover { filter: brightness(1.05); z-index: 2; }
.now { position: absolute; left: 0; right: 0; height: 2px; background: var(--red); z-index: 3; pointer-events: none; }
.now::before { content: ""; position: absolute; left: -5px; top: -4px; width: 10px; height: 10px; border-radius: 50%; background: var(--red); }

/* ---------- Dag ---------- */
.view-day { display: grid; grid-template-columns: minmax(0, 1fr) minmax(260px, 360px); gap: 1rem; align-items: start; }
.day-side { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem; }
.day-side-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; flex-wrap: wrap; margin-bottom: .5rem; }
.day-side h2 { font-size: 1rem; margin: 0; }
.day-card { border-left: 4px solid var(--c); background: var(--surface-2); border-radius: 8px; margin: .5rem 0; overflow: hidden; }
.day-card img { width: 100%; max-height: 180px; object-fit: cover; display: block; cursor: pointer; }
.day-card-body { padding: .5rem .6rem; position: relative; }
.day-card-title { border: none; background: none; padding: 0 2rem 0 0; font-weight: 600; text-align: left; font-size: .98rem; }
.day-card-body [data-speak-ev] { position: absolute; right: 4px; top: 4px; }
.notes { margin: .35rem 0 0; font-size: .85rem; white-space: pre-wrap; }
.datelink {
  border: none; background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); border-radius: 4px;
  padding: 0 4px; font-size: inherit; font-weight: 600;
}

/* ---------- FAB, toasts, popover ---------- */
.fab {
  position: fixed; right: calc(1.2rem + env(safe-area-inset-right)); bottom: calc(1.2rem + env(safe-area-inset-bottom)); z-index: 30;
  width: 58px; height: 58px; border-radius: 50%; border: none; background: var(--accent); color: var(--on-accent);
  font-size: 2rem; box-shadow: var(--shadow); line-height: 1;
}
.fab:hover { transform: scale(1.05); }
.toasts { position: fixed; left: 50%; bottom: calc(1rem + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: .5rem; width: min(440px, calc(100vw - 2rem)); }
.toast {
  background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); border-radius: var(--radius);
  padding: .7rem 2.4rem .7rem .9rem; position: relative; animation: in .2s ease-out;
}
.toast.reminder { border-left: 5px solid var(--accent); }
.toast.out { opacity: 0; transform: translateY(8px); transition: .25s; }
.toast-actions { display: flex; gap: .4rem; margin-top: .4rem; flex-wrap: wrap; }
.toast-actions:empty { display: none; }
.toast-close { position: absolute; right: 6px; top: 6px; }
@keyframes in { from { opacity: 0; transform: translateY(10px); } }

.popover {
  position: fixed; z-index: 40; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: .6rem .7rem .7rem;
}
.pop-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .4rem; }
.pop-actions { display: flex; gap: .4rem; margin-top: .6rem; }
.popover .chips { max-height: 220px; overflow-y: auto; }

/* ---------- Dialoger ---------- */
.dlg {
  border: none; border-radius: 16px; padding: 0; width: min(560px, calc(100vw - 1.5rem)); max-height: calc(100dvh - 1.5rem);
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.dlg.wide { width: min(720px, calc(100vw - 1.5rem)); }
.dlg::backdrop { background: rgba(10, 15, 25, .45); backdrop-filter: blur(2px); }
.dlg form, .dlg-wrap { display: flex; flex-direction: column; max-height: calc(100dvh - 1.5rem); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: .8rem 1rem .4rem 1.2rem; }
.dlg-head h2 { margin: 0; font-size: 1.15rem; }
.dlg-body { padding: .3rem 1.2rem 1rem; overflow-y: auto; display: flex; flex-direction: column; gap: .7rem; }
.dlg-foot { display: flex; gap: .5rem; padding: .7rem 1rem; border-top: 1px solid var(--line); flex-wrap: wrap; align-items: center; }
.field { display: flex; flex-direction: column; gap: .25rem; flex: 1; min-width: 0; }
.field > span { font-size: .78rem; font-weight: 600; color: var(--muted); }
.field input:not([type=checkbox]):not([type=color]):not([type=range]), .field select, .field textarea, #findText {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 8px; padding: .5em .7em; width: 100%; min-height: 40px;
}
.field input:focus, .field select:focus, .field textarea:focus, #findText:focus { outline: none; border-color: var(--accent); }
textarea { resize: vertical; }
input[type=color] { width: 44px; height: 36px; border: 1px solid var(--line); border-radius: 8px; padding: 2px; background: var(--surface-2); }
input[type=color]:disabled { opacity: .4; }
.row { display: flex; gap: .7rem; flex-wrap: wrap; }
.row .field { flex: 1 1 140px; }
.row .field.time { flex: 0 1 130px; }
.check { display: flex; align-items: center; gap: .5rem; font-size: .92rem; }
.check input, .pill input { width: 18px; height: 18px; accent-color: var(--accent); }
.colorpick { display: flex; align-items: center; gap: .6rem; min-height: 40px; }
.reminders { border: 1px solid var(--line); border-radius: 10px; padding: .4rem .7rem .7rem; display: flex; flex-wrap: wrap; gap: .4rem; }
.reminders legend { font-size: .78rem; font-weight: 600; color: var(--muted); padding: 0 .3rem; }
.pills { display: flex; flex-wrap: wrap; gap: .4rem; }
.pill { display: inline-flex; align-items: center; gap: .35rem; border: 1px solid var(--line); border-radius: 999px; padding: .25rem .7rem; font-size: .85rem; cursor: pointer; }
.pill:has(input:checked) { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }
.imgpick { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.ev-img { width: 100%; max-height: 200px; object-fit: cover; border-radius: 10px; }
.thumb { width: 64px; height: 44px; object-fit: cover; border-radius: 6px; }

#dlgSettings section { border-bottom: 1px solid var(--line); padding-bottom: 1rem; display: flex; flex-direction: column; gap: .6rem; align-items: flex-start; }
#dlgSettings section > .row, #dlgSettings section > .field { align-self: stretch; }
#dlgSettings section:last-child { border-bottom: none; }
#dlgSettings h3 { margin: .3rem 0 0; font-size: 1rem; }
#dlgSettings p { margin: 0; }
.month-imgs { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: .5rem; align-self: stretch; }
.month-img {
  position: relative; height: 64px; border-radius: 8px; background: var(--surface-2) center/cover; border: 1px dashed var(--line);
}
.month-img label { position: absolute; inset: 0; display: flex; align-items: flex-end; padding: 4px 6px; cursor: pointer; font-size: .78rem; font-weight: 600; }
.month-img[style] label { color: #fff; text-shadow: 0 1px 3px rgba(0, 0, 0, .8); }
.month-img button { position: absolute; top: 2px; right: 2px; background: rgba(0, 0, 0, .5); color: #fff; }
.cats { display: flex; flex-direction: column; gap: .4rem; align-self: stretch; }
.cat { display: flex; gap: .5rem; align-items: center; }
.cat input:not([type=color]) { flex: 1; border: 1px solid var(--line); background: var(--surface-2); border-radius: 8px; padding: .45em .7em; }

.find-results { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .5rem; }
.find-results li { display: flex; align-items: center; gap: .7rem; justify-content: space-between; background: var(--surface-2); border-radius: 10px; padding: .6rem .7rem; }
.find-results li > div { min-width: 0; }
.find-results .small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- Mobil ---------- */
@media (max-width: 760px) {
  .topbar { padding: .5rem .6rem; gap: .4rem; }
  #title { order: 2; flex-basis: calc(100% - 140px); font-size: 1.1rem; }
  .nav { order: 1; }
  .tools { order: 3; }
  .views { order: 4; flex-basis: 100%; justify-content: space-between; }
  .views button { flex: 1; }
  .quick { padding: .5rem .6rem; }
  .quick .btn { display: none; }
  .view { padding: 0 .4rem 5.5rem; }
  .month-grid { grid-template-columns: 1.6em repeat(7, minmax(0, 1fr)); grid-template-rows: auto repeat(var(--weeks), minmax(72px, 1fr)); min-height: calc(100dvh - 230px); }
  .chip { font-size: .66rem; padding: 0 3px; min-height: 16px; border-left-width: 2px; }
  .chip .t { display: none; }
  .hol { display: none; }
  .month-banner { height: 90px; }
  .view-day { grid-template-columns: 1fr; }
  .tg { height: calc(100dvh - 230px); }
  .view-day .tg { height: 60dvh; }
  .tg-head, .tg-allday, .tg-body { grid-template-columns: 40px repeat(var(--cols), minmax(0, 1fr)); }
  .tg-day b { font-size: 1rem; width: 28px; height: 28px; }
  .tg-ev { font-size: .65rem; padding: 1px 2px; }
  .tg-ev span { display: none; }
  .year-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: .5rem; }
  .mini-month { padding: .35rem; }
  .mini-grid { font-size: .7rem; }
}
@media print {
  .topbar .nav, .tools, .views, .quick, .fab, .toasts { display: none !important; }
  .tg, .month-grid { height: auto; min-height: 0; }
}
