/* ==========================================================================
   LIVING IT BUFFERTKALKYL
   Ett kassaböcker-lugnt verktyg: varm grafit (mörkt) eller papper (ljust),
   en accentfärg (guld), ett upp/ner-par (grön/röd). Siffrorna är huvudsaken.
   ========================================================================== */

@font-face {
  font-family: "Hanken Grotesk";
  src: url("/fonts/hanken-grotesk.woff2") format("woff2");
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Newsreader";
  src: url("/fonts/newsreader.woff2") format("woff2");
  font-weight: 200 800;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "JetBrains Mono";
  src: url("/fonts/jetbrains-mono.woff2") format("woff2");
  font-weight: 100 800;
  font-style: normal;
  font-display: swap;
}

:root {
  color-scheme: dark;
  --bg: #131416;
  --bg-elevated: #191b1e;
  --surface: #1a1c1f;
  --surface-2: #212428;
  --surface-hover: #2a2d32;
  --border: rgba(255, 255, 255, 0.09);
  --border-strong: rgba(255, 255, 255, 0.2);
  --shadow-float: 0 18px 44px -10px rgba(0, 0, 0, 0.7);
  --text: #ecebe6;
  --muted: #a8a8a0;
  --muted-2: #92928a;
  --accent: #e0b155;
  --accent-soft: rgba(224, 177, 85, 0.14);
  --on-accent: #1b1406;
  --focus: #e0b155;
  --focus-glow: rgba(224, 177, 85, 0.3);
  --positive: #7cc79a;
  --positive-soft: rgba(124, 199, 154, 0.13);
  --negative: #f0897f;
  --negative-soft: rgba(240, 137, 127, 0.14);
  /* Äldre alias: manuella värden använder accenten */
  --manual: var(--accent);
  --manual-soft: var(--accent-soft);
  /* Exakt rgba(255, 255, 255, 0.03) bevarad för Playwrights crosshair-test */
  --hl: rgba(255, 255, 255, 0.03);
  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 14px;
  --sans:
    "Hanken Grotesk", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --serif: "Newsreader", Georgia, "Times New Roman", serif;
  --mono:
    "JetBrains Mono", ui-monospace, "SF Mono", "Cascadia Code", Consolas,
    monospace;
  font-family: var(--sans);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  accent-color: var(--accent);
  scrollbar-color: var(--border-strong) transparent;
}

html[data-theme="light"] {
  color-scheme: light;
  --bg: #f5f3ee;
  --bg-elevated: #efece5;
  --surface: #fffdf9;
  --surface-2: #f8f5ee;
  --surface-hover: #ece8de;
  --border: #e3dfd4;
  --border-strong: #bdb8aa;
  --shadow-float: 0 18px 44px -12px rgba(40, 32, 12, 0.28);
  --text: #1d1c19;
  --muted: #5a5850;
  --muted-2: #66645b;
  --accent: #8a5b0a;
  --accent-soft: rgba(138, 91, 10, 0.1);
  --on-accent: #ffffff;
  --focus: #8a5b0a;
  --focus-glow: rgba(138, 91, 10, 0.22);
  --positive: #1b6e3f;
  --positive-soft: rgba(27, 110, 63, 0.1);
  --negative: #ad2a38;
  --negative-soft: rgba(173, 42, 56, 0.09);
  --hl: rgba(40, 32, 12, 0.045);
}

* {
  box-sizing: border-box;
}

::selection {
  background: var(--accent);
  color: var(--on-accent);
}
::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 999px;
  border: 2px solid var(--bg);
}

body {
  margin: 0;
  background-color: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
  caret-color: var(--accent);
}

/* ---------- Typografi ---------- */
h1 {
  font-family: var(--serif);
  font-size: 1.3rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  line-height: 1.15;
  margin: 0;
}
h1 span {
  color: var(--muted);
  font-weight: 400;
}
h2 {
  font-family: var(--serif);
  font-size: 1.25rem;
  font-weight: 600;
  letter-spacing: -0.005em;
  line-height: 1.2;
  margin: 0 0 0.75rem;
  text-wrap: balance;
}
h3 {
  font-size: 0.95rem;
  font-weight: 600;
  margin: 0;
}
.sub {
  color: var(--muted);
  margin: 0.25rem 0 0;
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}
kbd {
  font-family: var(--mono);
  font-size: 0.8rem;
  padding: 0.1rem 0.4rem;
  border-radius: 5px;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  color: var(--muted);
}

/* ---------- Kontroller ---------- */
select,
input,
button {
  font: inherit;
  color: var(--text);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.38rem 0.65rem;
  transition:
    border-color 0.15s ease,
    background-color 0.15s ease,
    color 0.15s ease;
}
input,
select {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: 0.9rem;
}
button {
  cursor: pointer;
  background: var(--surface-2);
  font-size: 0.85rem;
  font-weight: 500;
}
button:hover:not(:disabled) {
  border-color: var(--border-strong);
  background: var(--surface-hover);
}
button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
}
button.primary:hover:not(:disabled) {
  background: var(--accent);
  border-color: var(--text);
  filter: brightness(1.08);
}
button.ghost-btn {
  background: transparent;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}
button.ghost-btn:hover {
  color: var(--text);
  background: var(--surface-2);
}
button.danger-subtle:hover {
  border-color: var(--negative);
  color: var(--negative);
}
input:focus,
select:focus {
  outline: none;
  border-color: var(--focus);
  box-shadow: 0 0 0 3px var(--focus-glow);
  background: var(--surface);
}
:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
input::placeholder {
  color: var(--muted);
  opacity: 1;
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  font-size: 0.88rem;
  color: var(--muted);
  user-select: none;
}
.toggle:hover {
  color: var(--text);
}
.toggle input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 2.1rem;
  height: 1.2rem;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  position: relative;
  padding: 0;
  margin: 0;
  cursor: pointer;
}
.toggle input[type="checkbox"]::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 0.84rem;
  height: 0.84rem;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}
.toggle input[type="checkbox"]:checked {
  background: var(--accent-soft);
  border-color: var(--accent);
}
.toggle input[type="checkbox"]:checked::after {
  transform: translateX(0.9rem);
  background: var(--accent);
}

.select-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.88rem;
  font-weight: 500;
  color: var(--muted);
}
.select-pill select {
  font-family: var(--sans);
  font-weight: 700;
  font-size: 0.95rem;
  padding: 0.3rem 0.55rem;
  cursor: pointer;
}

/* ---------- Sidebar ---------- */
:root {
  --sidebar-w: 232px;
  --bottom-nav-h: 64px;
}
.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: var(--sidebar-w);
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  padding: 1.25rem 0.9rem;
  overflow-y: auto;
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 25;
}
.brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0 0.4rem 0.4rem;
}
.brand-mark {
  flex: none;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  background:
    linear-gradient(var(--on-accent), var(--on-accent)) 3px 3px / 8px 2px
      no-repeat,
    linear-gradient(var(--on-accent), var(--on-accent)) 3px 7px / 8px 2px
      no-repeat,
    var(--accent);
}
.side-nav {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}
.side-nav button,
.bottom-nav button {
  display: flex;
  align-items: center;
  gap: 0.7rem;
}
.side-nav button {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border-radius: var(--radius-sm);
  text-align: left;
  font-size: 0.95rem;
  background: transparent;
  border-color: transparent;
  color: var(--muted);
}
.side-nav button:hover:not(:disabled) {
  background: var(--surface-2);
  border-color: transparent;
  color: var(--text);
}
.side-nav button[aria-selected="true"] {
  background: var(--accent-soft);
  color: var(--text);
  font-weight: 600;
}
.nav-icon {
  flex: none;
  width: 1.2rem;
  height: 1.2rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}
[aria-selected="true"] > .nav-icon,
[aria-current] > .nav-icon {
  color: var(--accent);
}
#status {
  color: var(--muted);
  font-size: 0.85rem;
  min-height: 1.2rem;
  padding: 0 0.4rem;
}
#status:not(:empty)::before {
  content: "✓ ";
  color: var(--positive);
}
#status.error {
  color: var(--negative);
}
#status.error::before {
  content: "! ";
  color: var(--negative);
}
.account {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.4rem;
  margin-top: auto;
}
.account button {
  width: 100%;
  justify-content: space-between;
  text-align: left;
}

.bottom-nav,
.sidebar-scrim {
  display: none;
}

/* ---------- Huvudyta ---------- */
main {
  margin-left: var(--sidebar-w);
  padding: 1.25rem 2rem 4rem;
  max-width: 1500px;
}
.topbar {
  display: flex;
  align-items: center;
  min-height: 2.6rem;
  margin-bottom: 1.25rem;
}
.topbar:has(.controls[hidden]) {
  display: none;
}
.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem 1.5rem;
}
.controls[hidden] {
  display: none;
}

/* Hero: svaret på "är jag trygg?" */
.hero {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr);
  gap: 2rem 3.5rem;
  padding: 0.5rem 0 1.75rem;
  border-bottom: 1px solid var(--border);
  align-items: start;
}
.hero-line {
  margin: 0;
  font-family: var(--serif);
  font-size: clamp(2rem, 3.4vw, 3.1rem);
  font-weight: 500;
  line-height: 1.12;
  letter-spacing: -0.02em;
  text-wrap: balance;
}
.hero-line .big {
  font-family: var(--serif);
  font-weight: 700;
  font-size: inherit;
  letter-spacing: inherit;
  margin: 0;
  white-space: nowrap;
  color: var(--positive);
}
.hero-line[data-tone="mid"] .big {
  color: var(--accent);
}
.hero-line[data-tone="low"] .big {
  color: var(--negative);
}
.hero-main .sub {
  margin-top: 0.75rem;
  font-size: 0.95rem;
}
.hero-buffer h2 {
  font-family: var(--sans);
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--muted);
  margin: 0 0 0.2rem;
}
.hero-buffer .big {
  font-size: 1.7rem;
}

.big {
  font-family: var(--serif);
  font-size: 1.55rem;
  font-weight: 600;
  letter-spacing: -0.015em;
  line-height: 1.2;
  margin: 0.1rem 0 0;
  font-variant-numeric: lining-nums tabular-nums;
}

/* Nyckeltal som liggande liggare, inte kort */
.ledger {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(175px, 1fr));
  margin: 0 0 1.75rem;
  border-bottom: 1px solid var(--border);
}
.ledger-item {
  display: flex;
  flex-direction: column;
  padding: 1.1rem 1.25rem 1.1rem 0;
  min-width: 0;
}
.ledger dt {
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 500;
}
.ledger dd {
  margin: 0;
}
.ledger .big {
  font-size: 1.4rem;
}

.spark {
  margin-top: auto;
  padding-top: 0.6rem;
  color: var(--muted-2);
}
.hero-buffer .spark {
  color: var(--accent);
  padding-top: 0.9rem;
}
.spark svg {
  display: block;
  width: 100%;
  max-width: 220px;
  height: 30px;
  overflow: visible;
}
.hero-buffer .spark svg {
  max-width: none;
  height: 44px;
}
.spark-area {
  stroke: none;
  opacity: 0.22;
}
.spark-line {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linejoin: round;
  stroke-linecap: round;
  vector-effect: non-scaling-stroke;
}
.spark-dot {
  fill: currentColor;
}

/* Paneler och kort: plana ytor */
.card,
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.3rem;
}
.card {
  display: flex;
  flex-direction: column;
}
.card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.3rem;
}
.card h2 {
  font-family: var(--sans);
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 500;
  margin: 0;
}
.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1rem;
  margin-bottom: 1.25rem;
}
.panel {
  overflow-x: auto;
}

/* ---------- Diagram ---------- */
.chart-panel {
  margin-bottom: 1.5rem;
  padding: 1.25rem 1.5rem;
}
.chart-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.8rem;
}
.chart-sub {
  margin-top: -0.4rem;
}
.legend {
  display: flex;
  gap: 1.25rem;
  margin: 0 0 0.6rem;
  font-size: 0.85rem;
  color: var(--muted);
}
.chart-head .legend {
  margin: 0;
}
.key::before {
  content: "";
  display: inline-block;
  width: 1.1rem;
  height: 0;
  margin-right: 0.5rem;
  vertical-align: middle;
  border-top: 2px dashed var(--muted);
}
.key.avstamd::before {
  border-top: 3px solid var(--accent);
}

#buffer-chart svg {
  display: block;
  width: 100%;
  height: auto;
  color: var(--muted);
}
#buffer-chart .grid-line,
.bar-chart .grid-line {
  stroke: var(--border);
  stroke-width: 1;
}
#buffer-chart .zero-line {
  stroke: var(--border-strong);
  stroke-dasharray: 4 4;
}
#buffer-chart text,
.bar-chart text {
  fill: var(--muted);
  font-family: var(--sans);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
#buffer-chart .line-prognos {
  fill: none;
  stroke: var(--muted);
  stroke-width: 2;
  stroke-dasharray: 6 4;
  stroke-linejoin: round;
}
#buffer-chart .line-avstamd {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.6;
  stroke-linejoin: round;
}
#buffer-chart .dot-prognos,
#buffer-chart .dot-avstamd {
  fill: var(--bg);
  stroke-width: 2.25;
  transition: r 0.15s ease;
  cursor: pointer;
}
#buffer-chart .dot-prognos {
  stroke: var(--muted);
}
#buffer-chart .dot-avstamd {
  stroke: var(--accent);
}
#buffer-chart .dot-prognos:hover,
#buffer-chart .dot-avstamd:hover {
  r: 5.5;
}
.bar-chart svg {
  width: 100%;
  height: auto;
  display: block;
}
.bar-chart .bar-a {
  fill: var(--muted-2);
  rx: 3px;
}
.bar-chart .bar-b {
  fill: var(--accent);
  rx: 3px;
}
.bar-chart rect:hover {
  opacity: 0.85;
}
.bar-chart .bar-value {
  fill: var(--text);
}

.last-reconciled {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.35rem 0.9rem;
  margin: 0 0 0.85rem;
}
.last-reconciled[hidden] {
  display: none;
}
.lr-label,
.lr-date {
  font-size: 0.88rem;
  color: var(--muted);
}
.last-reconciled strong {
  font-family: var(--serif);
  font-size: 1.6rem;
  letter-spacing: -0.015em;
  font-variant-numeric: lining-nums tabular-nums;
}
.lr-chip {
  font-size: 0.85rem;
  padding: 0.1rem 0.6rem;
  border-radius: 999px;
  background: var(--positive-soft);
  color: var(--positive);
}
.lr-chip.neg {
  background: var(--negative-soft);
  color: var(--negative);
}
#lr-runway {
  background: var(--accent-soft);
  color: var(--accent);
}

/* ---------- Verktyg: simulator och årsinställningar ---------- */
.tools {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.8rem 1.25rem;
  margin-bottom: 1.5rem;
}
.tools summary {
  cursor: pointer;
  color: var(--muted);
  font-weight: 500;
  user-select: none;
}
.tools summary:hover {
  color: var(--text);
}
.tools[open] summary {
  color: var(--text);
  margin-bottom: 1rem;
}
.tools-h {
  margin-top: 1.5rem;
  font-size: 1.05rem;
}
.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 0.85rem;
}
.settings-grid label,
.template-form label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--muted);
}
.sim {
  padding-top: 0.25rem;
}
.sim-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
}
.sim h2 {
  margin: 0;
  font-size: 1.05rem;
}
.sim-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}
.sim-buttons button[data-sim] {
  min-width: 4.4rem;
  font-family: var(--mono);
}
.sim-buttons button[data-sim]:hover:not(:disabled) {
  border-color: var(--positive);
  color: var(--positive);
  background: var(--positive-soft);
}
.sim-buttons button[data-sim^="-"]:hover:not(:disabled) {
  border-color: var(--negative);
  color: var(--negative);
  background: var(--negative-soft);
}
.sim-result {
  margin: 0.6rem 0 0;
  font-size: 0.95rem;
}
.sim-result strong {
  color: var(--positive);
  font-size: 1.05rem;
  margin: 0 0.25rem;
}
.sim-result #sim-monthly {
  display: block;
}

/* ---------- Årsmatrisen (#grid) ---------- */
.scroll {
  overflow: auto;
  max-height: calc(100vh - 6.5rem);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
}
table {
  border-collapse: collapse;
  width: 100%;
  font-variant-numeric: tabular-nums;
}
th,
td {
  padding: 0.4rem 0.6rem;
  text-align: left;
  white-space: nowrap;
}
td,
.num {
  font-family: var(--mono);
  font-size: 0.88rem;
}
td.num,
th.num,
#grid td,
#grid thead th:not(:first-child) {
  text-align: right;
}
#grid {
  min-width: 1320px;
  border-collapse: separate;
  border-spacing: 0;
}
#grid th:first-child {
  position: sticky;
  left: 0;
  background: var(--surface);
  font-weight: 500;
  font-size: 0.9rem;
  z-index: 1;
  min-width: 14.5rem;
  border-right: 1px solid var(--border-strong);
}
#grid thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface-2);
  color: var(--muted);
  font-weight: 600;
  font-size: 0.8rem;
  padding-top: 0.6rem;
  padding-bottom: 0.6rem;
  border-bottom: 1px solid var(--border-strong);
  text-align: right;
}
#grid thead th:first-child {
  z-index: 3;
  text-align: left;
}
#grid tbody tr:hover th {
  background: var(--surface-2);
  color: var(--text);
}
/* Crosshair: aktiv rad och månadskolumn */
#grid tbody tr:hover td,
#grid td.xh-col {
  background-color: var(--hl);
}
#grid thead th.xh-col {
  background: var(--surface-hover);
  color: var(--text);
}
#grid tr.sep td,
#grid tr.sep th {
  border-top: 1px solid var(--border-strong);
}
#grid tr[data-row="in"] td,
#grid tr[data-row="in"] th,
#grid tr[data-row="prognos"] td,
#grid tr[data-row="prognos"] th {
  font-weight: 600;
}
#grid td.sum,
#grid th.sum {
  font-weight: 700;
  border-left: 1px solid var(--border-strong);
}
#grid tr.playground th:first-child {
  color: var(--accent);
}
#grid input,
#grid select {
  width: 5.5rem;
  text-align: right;
  padding: 0.2rem 0.4rem;
  background: transparent;
  border-color: var(--border);
  border-radius: 5px;
  font-size: 0.88rem;
}
#grid input:hover:not(:disabled),
#grid select:hover:not(:disabled) {
  border-color: var(--border-strong);
  background: var(--bg-elevated);
}
#grid input:focus,
#grid select:focus {
  background: var(--bg);
  border-color: var(--focus);
}
#grid select {
  width: 6.4rem;
  text-align: left;
}
#grid select.model-75 {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-soft);
}
#grid input.manual {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}
#grid td.manual {
  color: var(--accent);
}
#grid .row-actions {
  display: inline-flex;
  gap: 0.25rem;
  margin-left: 0.45rem;
}
#grid .row-actions button {
  padding: 0.1rem 0.45rem;
  font-size: 0.8rem;
  border-radius: 4px;
}
.neg,
#grid input.neg {
  color: var(--negative);
}
.pos {
  color: var(--positive);
}
.mname {
  margin-right: 0.25rem;
}
.lock-btn {
  padding: 0.15rem 0.3rem;
  line-height: 0;
  vertical-align: middle;
  color: var(--muted);
  background: transparent;
  border-color: transparent;
}
.lock-btn:hover {
  color: var(--text);
  background: transparent;
}
.lock-btn[aria-pressed="true"] {
  color: var(--accent);
}
.recon-badge {
  color: var(--positive);
  font-size: 0.85rem;
  margin-right: 0.15rem;
}
#grid .col-locked input:disabled,
#grid .col-locked select:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
#grid thead th.col-locked {
  box-shadow: inset 0 -2px 0 var(--accent);
}

/* Formelförklaring */
#grid td[tabindex]:focus-visible,
#grid td[aria-describedby] {
  outline: 1px dashed var(--focus);
  outline-offset: -2px;
  cursor: help;
}
#xray {
  position: fixed;
  z-index: 60;
  max-width: min(27rem, calc(100vw - 16px));
  padding: 0.8rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow-float);
  pointer-events: none;
  font-size: 0.9rem;
}
#xray[hidden] {
  display: none;
}
#xray strong {
  display: block;
  font-size: 0.85rem;
  color: var(--accent);
}
#xray p {
  margin: 0.32rem 0 0;
}
.xray-formula {
  color: var(--muted);
}
.xray-calc {
  font-family: var(--mono);
  color: var(--text);
  font-weight: 600;
  overflow-wrap: anywhere;
}
.xray-note {
  color: var(--muted);
  font-size: 0.85rem;
}

@keyframes saved-flash {
  0% {
    border-color: var(--positive);
    box-shadow: 0 0 0 3px var(--positive-soft);
  }
  100% {
    box-shadow: 0 0 0 0 transparent;
  }
}
.saved-flash {
  animation: saved-flash 0.9s ease-out;
}

/* ---------- Nedre paneler ---------- */
.bottom {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 480px), 1fr));
  gap: 1.25rem;
  margin-top: 1.5rem;
  align-items: start;
}
.panel thead th {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 600;
  padding-bottom: 0.45rem;
}
.panel tbody tr,
.panel tfoot tr {
  border-top: 1px solid var(--border);
}
.panel tbody tr:hover {
  background: var(--hl);
}
.panel input {
  width: 100%;
}
.panel td input {
  text-align: right;
}
.panel td.num input {
  max-width: 9rem;
}
.panel tfoot th,
.panel tfoot td {
  font-weight: 700;
  padding-top: 0.6rem;
}
.panel tfoot th {
  text-align: left;
}
.panel td.wrap {
  white-space: normal;
  font-family: var(--sans);
}
.list-scroll {
  max-height: 22rem;
  overflow: auto;
}
.list-scroll thead th {
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}
.list-scroll tfoot th,
.list-scroll tfoot td {
  position: sticky;
  bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--border-strong);
}
#purchases button[data-delete] {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  text-decoration: underline;
  text-underline-offset: 3px;
  padding: 0.2rem 0.4rem;
}
#purchases button[data-delete]:hover {
  color: var(--negative);
  background: transparent;
}
.inline-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.85rem;
  padding-top: 0.85rem;
  border-top: 1px solid var(--border);
}
.inline-form input[name="description"] {
  flex: 2 1 160px;
  font-family: var(--sans);
}
.inline-form input[name="amount"] {
  flex: 1 1 110px;
}
.vat-buttons {
  display: inline-flex;
  gap: 0.3rem;
}
.vat-buttons button {
  font-size: 0.85rem;
  font-family: var(--mono);
  padding: 0.3rem 0.5rem;
}

.tax-guard {
  margin-top: 1rem;
  padding: 1rem 1.05rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.tax-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 0.5rem;
}
.tax-head span {
  color: var(--muted);
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
}
.progress {
  height: 0.6rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.3s ease;
}
.progress.over .progress-fill {
  background: var(--negative);
}
.tax-guard button {
  margin-top: 0.65rem;
}

/* ---------- Månadskalkyl och historik ---------- */
.template-form {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(185px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1rem;
}
#breakdown th,
#net-salary th,
#arbitrage th,
#yoy-table th[scope="row"],
#yoy-categories th[scope="row"] {
  font-weight: 400;
  color: var(--muted);
  text-align: left;
}
#breakdown td,
#net-salary td,
#arbitrage td,
#yoy-table td,
#yoy-categories td {
  text-align: right;
}
#breakdown tr,
#net-salary tr,
#arbitrage tr,
#yoy-table tr,
#yoy-categories tr,
#matrix tbody tr {
  border-top: 1px solid var(--border);
}
#breakdown tr.total th,
#breakdown tr.total td,
#net-salary tr.total th,
#net-salary tr.total td,
#arbitrage tr.total th,
#arbitrage tr.total td {
  color: var(--text);
  font-weight: 700;
  font-size: 1rem;
}
#matrix tr.current {
  background: var(--accent-soft);
}
#matrix tr.current td {
  color: var(--accent);
  font-weight: 600;
}
#yoy-table tfoot td,
#yoy-categories tfoot td {
  font-weight: 700;
}
.row-drift th,
.row-drift td {
  font-size: 0.85rem;
  color: var(--muted);
}

/* ---------- Inloggning, dialoger, kommandopalett, toast ---------- */
.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.25rem;
}
.login-card {
  width: min(100%, 24rem);
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 2.1rem;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-float);
}
.login-brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}
.login-card label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.88rem;
  color: var(--muted);
}
.login-card .sub {
  margin: -0.45rem 0 0;
}
.login-card button {
  padding: 0.6rem;
}
.login-error {
  margin: 0;
  min-height: 1.2em;
  color: var(--negative);
  font-size: 0.9rem;
}
dialog {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  padding: 0;
  width: min(92vw, 28rem);
  box-shadow: var(--shadow-float);
}
dialog::backdrop {
  background: rgba(10, 10, 10, 0.6);
}
.dialog-card {
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}
.dialog-card p {
  margin: 0;
}
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.4rem;
}
#cmd-dialog {
  width: min(92vw, 35rem);
  margin-top: 12vh;
}
.cmd-card {
  display: flex;
  flex-direction: column;
}
.cmd-search-wrap {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.9rem 1.05rem;
  border-bottom: 1px solid var(--border);
}
#cmd-input {
  flex: 1;
  border: none;
  background: transparent;
  font-family: var(--sans);
  font-size: 1rem;
  padding: 0.2rem;
}
#cmd-input:focus {
  box-shadow: none;
}
.cmd-list {
  max-height: 21rem;
  overflow-y: auto;
  padding: 0.45rem;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}
.cmd-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  text-align: left;
  padding: 0.6rem 0.8rem;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-family: var(--sans);
  font-size: 0.92rem;
  color: var(--text);
}
.cmd-item:hover,
.cmd-item.active {
  background: var(--surface-2);
  border-color: var(--border-strong);
}
.cmd-item span.cmd-cat {
  font-size: 0.85rem;
  color: var(--muted);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.5rem;
  transform: translateX(-50%);
  z-index: 70;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.6rem 0.7rem 0.6rem 1.1rem;
  background: var(--text);
  color: var(--bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow-float);
  font-size: 0.92rem;
}
.toast[hidden] {
  display: none;
}
.toast button {
  background: transparent;
  color: inherit;
  border-color: currentColor;
  font-weight: 600;
}
.toast button:hover:not(:disabled) {
  background: transparent;
  border-color: currentColor;
  filter: brightness(1.2);
}

/* ---------- Privat läge ---------- */
body.privacy .big,
body.privacy .hero-line .big,
body.privacy .sub,
body.privacy .spark,
body.privacy #grid tbody td,
body.privacy #purchases td,
body.privacy #planning td,
body.privacy .sim-result strong,
body.privacy #wellness-text,
body.privacy #buffer-chart svg,
body.privacy .settings-grid input,
body.privacy .last-reconciled strong {
  filter: blur(7px);
  transition: filter 0.15s;
}
body.privacy .big:hover,
body.privacy .sub:hover,
body.privacy .spark:hover,
body.privacy #grid tbody td:hover,
body.privacy #grid tbody td:focus-within,
body.privacy #purchases td:hover,
body.privacy #planning td:hover,
body.privacy .sim-result strong:hover,
body.privacy #wellness-text:hover,
body.privacy #buffer-chart svg:hover,
body.privacy .settings-grid input:focus,
body.privacy .settings-grid input:hover,
body.privacy .last-reconciled strong:hover {
  filter: none;
}

/* ---------- Smal skärm ---------- */
@media (max-width: 1100px) {
  .hero {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 860px) {
  main {
    margin-left: 0;
    padding: 0.9rem 1rem calc(var(--bottom-nav-h) + 2rem);
  }
  .hero-line {
    font-size: 2rem;
  }
  .ledger {
    grid-template-columns: 1fr 1fr;
  }
  .ledger-item {
    padding-right: 0.75rem;
  }
  .sidebar {
    inset: auto 0 var(--bottom-nav-h) 0;
    width: auto;
    max-height: 78vh;
    border-right: 0;
    border-top: 1px solid var(--border-strong);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    transform: translateY(110%);
    visibility: hidden;
    transition:
      transform 0.25s cubic-bezier(0.16, 1, 0.3, 1),
      visibility 0.25s;
    z-index: 40;
  }
  .sidebar.open {
    transform: none;
    visibility: visible;
  }
  .sidebar .side-nav {
    display: none;
  }
  .sidebar-scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(10, 10, 10, 0.55);
    z-index: 30;
  }
  .sidebar-scrim[hidden] {
    display: none;
  }
  .bottom-nav {
    position: fixed;
    inset: auto 0 0 0;
    height: var(--bottom-nav-h);
    padding-bottom: env(safe-area-inset-bottom);
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    background: var(--surface);
    border-top: 1px solid var(--border);
    z-index: 50;
  }
  .bottom-nav button {
    flex-direction: column;
    justify-content: center;
    gap: 0.15rem;
    background: transparent;
    border: 0;
    border-radius: 0;
    font-size: 0.8rem;
    color: var(--muted);
  }
  .bottom-nav button[aria-current],
  .bottom-nav button[aria-expanded="true"] {
    color: var(--text);
  }
  .last-reconciled strong {
    font-size: 1.35rem;
  }
  .toast {
    bottom: calc(var(--bottom-nav-h) + 0.75rem);
    width: calc(100vw - 2rem);
    justify-content: space-between;
  }
  /* Matrisen scrollar i sin egen ruta; månadsnamnskolumnen blir smalare */
  #grid th:first-child {
    min-width: 9.5rem;
  }
  .topbar {
    margin-bottom: 0.75rem;
  }
}

/* Pekskärmar och smala skärmar: minst 44px träffyta */
@media (pointer: coarse), (max-width: 860px) {
  button,
  select,
  .select-pill select,
  .inline-form input {
    min-height: 44px;
  }
  .toggle input[type="checkbox"] {
    min-height: 0;
  }
  #grid .row-actions button,
  .lock-btn {
    min-width: 44px;
    min-height: 44px;
  }
  .toggle {
    min-height: 44px;
  }
  .panel td input {
    min-height: 44px;
  }
  #grid input,
  #grid select {
    min-height: 40px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
  .saved-flash {
    border-color: var(--positive);
  }
}

@media print {
  .sidebar,
  .bottom-nav,
  .topbar,
  .toast,
  .tools,
  #purchase-form,
  #fill-to-threshold {
    display: none !important;
  }
  main {
    margin-left: 0;
  }
  body {
    background: #ffffff;
    color: #000000;
  }
  .scroll,
  .card,
  .panel {
    border-color: #cccccc;
    max-height: none;
    overflow: visible;
  }
}
