:root {
  --bg: #f6f2ee;
  --surface: #ffffff;
  --surface-2: #f0ebe6;
  --text: #241c19;
  --muted: #7a6d66;
  --line: #e2d9d2;
  --brand: #6b1f2e;
  --brand-soft: #f3e3e6;
  --empty: #ece6e0;
  --empty-line: #d9cfc7;
  --filled: #4f8a5f;
  --filled-text: #ffffff;
  --over: #d6453d;
  --hit: #e8a317;
  --radius: 12px;
  --cell-radius: 5px;
  --shadow: 0 1px 2px rgba(40, 20, 10, .06), 0 4px 16px rgba(40, 20, 10, .06);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16110f;
    --surface: #211a17;
    --surface-2: #2a221e;
    --text: #f1e9e3;
    --muted: #a89a91;
    --line: #3a302b;
    --brand: #c9566b;
    --brand-soft: #3a2027;
    --empty: #2c2420;
    --empty-line: #41362f;
    --filled: #5d9e6e;
    --over: #ef5a50;
    --hit: #f2b632;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16110f; --surface: #211a17; --surface-2: #2a221e; --text: #f1e9e3; --muted: #a89a91;
  --line: #3a302b; --brand: #c9566b; --brand-soft: #3a2027; --empty: #2c2420; --empty-line: #41362f;
  --filled: #5d9e6e; --over: #ef5a50; --hit: #f2b632; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }
a { color: var(--brand); }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- topp ---------- */
.top {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: calc(10px + var(--safe-t)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand img { border-radius: 7px; }
.brand h1 { font-size: 17px; margin: 0; letter-spacing: -.01em; }
.search {
  flex: 1; display: flex; align-items: center; gap: 6px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 999px;
  padding: 0 6px 0 12px; height: 38px; max-width: 520px;
}
.search svg { width: 17px; height: 17px; fill: none; stroke: var(--muted); stroke-width: 2; flex: none; }
.search input {
  flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--text);
  font: inherit; font-size: 16px; /* 16px hindrar iOS från att zooma */
}
.search input::-webkit-search-cancel-button { display: none; }
.status { display: flex; align-items: center; gap: 6px; margin-left: auto; white-space: nowrap; }
.status #updated { font-size: 13px; }
.icon-btn {
  display: inline-grid; place-items: center; width: 34px; height: 34px; flex: none;
  border: 0; border-radius: 999px; background: transparent; cursor: pointer; font-size: 17px;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.spin svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.warn-dot { color: var(--over); font-weight: 700; }

.banner {
  margin: 10px 16px 0; padding: 9px 12px; border-radius: 10px;
  background: var(--brand-soft); color: var(--text); font-size: 14px;
}

/* ---------- huvudlayout: dator och iPad liggande (karta + sidopanel) ---------- */
.layout { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 16px; padding: 16px; align-items: start; }
.map-wrap { min-width: 0; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 13px; color: var(--muted); margin: 0 2px 10px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.sw { width: 14px; height: 14px; border-radius: 4px; display: inline-block; }
.sw.empty { background: var(--empty); border: 1px solid var(--empty-line); }
.sw.filled { background: var(--filled); }
.sw.over { background: var(--over); }
.sw.hit { background: transparent; box-shadow: inset 0 0 0 2.5px var(--hit); }

.map {
  display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 12px;
  grid-auto-flow: dense;
}
.sec {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px; box-shadow: var(--shadow); min-width: 0;
}
.sec h3 {
  margin: 0 0 10px; font-size: 13px; font-weight: 600; color: var(--muted);
  display: flex; justify-content: space-between; gap: 8px;
}
.sec h3 .cnt { font-weight: 500; font-variant-numeric: tabular-nums; }

/* rutnät med en flaska per plats */
.grid {
  display: grid; gap: 3px;
  grid-template-columns: 18px repeat(var(--cols), minmax(0, 44px));
  justify-content: start;
}
.grid .lbl {
  font-size: 10.5px; color: var(--muted); display: grid; place-items: center;
  font-variant-numeric: tabular-nums;
}
.cell {
  aspect-ratio: 1; min-width: 0; border: 0; padding: 0; cursor: pointer;
  border-radius: var(--cell-radius); background: var(--empty);
  box-shadow: inset 0 0 0 1px var(--empty-line);
  display: grid; place-items: center; font-size: 11px; font-weight: 700; color: var(--filled-text);
  transition: transform .08s ease, opacity .15s ease;
}
.cell:hover { transform: scale(1.08); }
.cell.filled { background: var(--filled); box-shadow: none; }
.cell.over { background: var(--over); box-shadow: none; }

/* fack som rymmer flera flaskor */
.boxes { display: grid; grid-template-columns: repeat(auto-fill, minmax(76px, 1fr)); gap: 6px; }
.box {
  border: 0; cursor: pointer; text-align: left; padding: 7px 8px; border-radius: 8px; min-height: 50px;
  background: var(--empty); box-shadow: inset 0 0 0 1px var(--empty-line); color: var(--text);
  display: flex; flex-direction: column; justify-content: space-between; gap: 2px;
  transition: transform .08s ease, opacity .15s ease;
}
.box:hover { transform: translateY(-1px); }
.box .nm { font-size: 11.5px; line-height: 1.15; word-break: break-word; }
.box .n { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; }
.box.filled { background: var(--filled); color: var(--filled-text); box-shadow: none; }
.box.over { background: var(--over); color: #fff; box-shadow: none; }

/* sökning och markering */
.map.searching .cell, .map.searching .box { opacity: .28; }
.map.searching .cell.hit, .map.searching .box.hit { opacity: 1; }
.cell.hit, .box.hit { box-shadow: 0 0 0 2.5px var(--hit) !important; }
.cell.sel, .box.sel { outline: 3px solid var(--text); outline-offset: 1px; opacity: 1 !important; }

/* ---------- panel ---------- */
.panel {
  position: sticky; top: 74px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); max-height: calc(100vh - 90px); display: flex; flex-direction: column;
}
.panel-head { display: flex; align-items: center; gap: 6px; padding: 12px 12px 8px 16px; border-bottom: 1px solid var(--line); }
.panel-head h2 { font-size: 16px; margin: 0; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.panel-body { overflow-y: auto; padding: 8px 16px 16px; -webkit-overflow-scrolling: touch; }
.sheet-only, .sheet-handle, .bottom-bar { display: none; }

.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 8px 0 14px; }
.kpi { background: var(--surface-2); border-radius: 10px; padding: 9px 10px; }
.kpi b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.kpi span { font-size: 12px; color: var(--muted); }
.group-title { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 16px 0 6px; }
.group-title .badge { margin-left: 4px; }
.badge {
  display: inline-block; min-width: 20px; padding: 1px 6px; border-radius: 999px; font-size: 12px; text-align: center;
  background: var(--surface-2); color: var(--text); font-weight: 600; letter-spacing: 0; text-transform: none;
}
.badge.red { background: var(--over); color: #fff; }

.list { list-style: none; margin: 0; padding: 0; }
.list li + li { border-top: 1px solid var(--line); }
.row { text-decoration: none; color: inherit;
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  border: 0; background: transparent; padding: 9px 2px; cursor: pointer;
}
.row:hover { background: var(--surface-2); }
.row .main { flex: 1; min-width: 0; }
.row .t { display: block; font-weight: 550; overflow: hidden; text-overflow: ellipsis; }
.row .s { display: block; font-size: 13px; color: var(--muted); }
.row .q { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; white-space: nowrap; }
.row .chev { color: var(--muted); }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; background: var(--muted); }
.dot.Red { background: #8e2236; }
.dot.White { background: #e5d27a; }
.dot.Rosé, .dot.Rose { background: #ec9aa8; }
.dot.Sparkling { background: #d8c89a; box-shadow: inset 0 0 0 2px #fff8; }
.empty-note { color: var(--muted); padding: 10px 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 2px; }
.chip {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 999px; padding: 3px 10px;
  font-size: 13px; cursor: pointer;
}
.chip.over { border-color: var(--over); color: var(--over); font-weight: 600; }

.bin-status { display: flex; align-items: center; gap: 8px; margin: 10px 0 4px; font-size: 14px; }
.bin-status .pill { padding: 3px 10px; border-radius: 999px; font-weight: 600; font-size: 13px; }
.pill.ok { background: var(--filled); color: #fff; }
.pill.over { background: var(--over); color: #fff; }
.pill.empty { background: var(--empty); }

/* vinkort */
.wine h3 { font-size: 19px; line-height: 1.25; margin: 10px 0 2px; }
.wine .sub { color: var(--muted); margin: 0 0 12px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0 0 14px; font-size: 14px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }
.window { display: inline-flex; gap: 6px; align-items: center; }
.window small { color: var(--muted); }
.ct-link {
  display: flex; justify-content: center; align-items: center; gap: 8px; margin-top: 16px;
  background: var(--brand); color: #fff; text-decoration: none; padding: 12px; border-radius: 12px; font-weight: 600;
}
.note { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; font-size: 14px; margin: 4px 0; }

/* ---------- smalare än iPad liggande (alla iPads liggande är minst 1024 px): panelen som ark ---------- */
@media (max-width: 1023px) {
  .layout { grid-template-columns: 1fr; padding-bottom: calc(80px + var(--safe-b)); }
  .panel {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto; z-index: 30;
    max-height: 72vh; border-radius: 18px 18px 0 0; border-bottom: 0;
    transform: translateY(105%); transition: transform .25s ease;
    padding-bottom: var(--safe-b); box-shadow: 0 -8px 30px rgba(0,0,0,.18);
  }
  .panel.open { transform: none; }
  .sheet-only { display: inline-grid; }
  .sheet-handle { display: block; width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 8px auto 0; }
  .bottom-bar {
    display: flex; justify-content: center; position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
    padding: 10px 16px calc(10px + var(--safe-b));
    background: color-mix(in srgb, var(--bg) 90%, transparent);
    backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-top: 1px solid var(--line);
  }
  .bottom-bar button {
    border: 0; background: var(--brand); color: #fff; border-radius: 999px; padding: 10px 22px; font-weight: 600; cursor: pointer;
  }
  .bottom-bar .badge { background: #fff; color: var(--brand); }
}

/* ---------- telefon ---------- */
@media (max-width: 699px) {
  .top { flex-wrap: wrap; gap: 8px 10px; padding-left: 16px; padding-right: 10px; }
  .search { order: 3; flex-basis: 100%; max-width: none; margin-right: 6px; }
  .layout { padding: 12px 16px calc(80px + var(--safe-b)); }
  .map { grid-template-columns: 1fr; gap: 10px; }
  .map .sec { grid-column: auto !important; grid-row: auto !important; }
  .grid { gap: 3px; }
  .boxes { grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); }
  .legend { font-size: 12px; gap: 4px 12px; }
}

/* ---------- inloggning ---------- */
.login-body { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login {
  width: 100%; max-width: 340px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 18px; padding: 28px 24px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 8px;
}
.login img { border-radius: 14px; align-self: center; }
.login h1 { text-align: center; font-size: 22px; margin: 6px 0 12px; }
.login label { font-size: 13px; color: var(--muted); }
.login input {
  font: inherit; font-size: 16px; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text);
}
.login button {
  margin-top: 6px; border: 0; border-radius: 10px; padding: 12px; background: var(--brand); color: #fff;
  font-weight: 600; cursor: pointer;
}
.login .error { color: var(--over); min-height: 1em; margin: 0; font-size: 14px; }
.login .small { text-align: center; margin: 6px 0 0; }
