:root {
  --bg: #0f1115;
  --bg-elev: #171a21;
  --bg-elev-2: #1f232c;
  --line: #2a2f3a;
  --text: #e7e9ee;
  --muted: #9aa3b2;
  --primary: #6366f1;
  --primary-hover: #5457e6;
  --danger: #e5484d;
  --radius: 16px;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* the hidden attribute must always win over class-level display rules */
[hidden] { display: none !important; }

html {
  -webkit-text-size-adjust: 100%;   /* stop iOS auto-inflating text in landscape */
  text-size-adjust: 100%;
}

html, body {
  margin: 0;
  padding: 0;
  overflow-x: hidden;               /* never let the page scroll sideways on phones */
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(99, 102, 241, 0.18), transparent 60%),
    radial-gradient(900px 500px at -10% 10%, rgba(34, 197, 94, 0.10), transparent 55%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font-family: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}

.btn { white-space: nowrap; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 18px clamp(16px, 4vw, 48px);
  padding-top: max(18px, env(safe-area-inset-top));
  padding-left: max(clamp(16px, 4vw, 48px), env(safe-area-inset-left));
  padding-right: max(clamp(16px, 4vw, 48px), env(safe-area-inset-right));
  max-width: 1180px;
  margin: 0 auto;
}
.brand { min-width: 0; }            /* allow the brand text to shrink/ellipsis */
.brand-text { min-width: 0; }
.brand-text h1, .brand-text p { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { flex-shrink: 0; }
.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark {
  width: 46px; height: 46px;
  display: grid; place-items: center;
  font-weight: 800; font-size: 22px;
  color: #fff;
  background: linear-gradient(135deg, var(--primary), #22c55e);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.brand-text h1 { margin: 0; font-size: 20px; font-weight: 800; letter-spacing: -0.02em; }
.brand-text p { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); }
.topbar-actions { display: flex; align-items: center; gap: 12px; }
.admin-badge {
  font-size: 12px; font-weight: 600;
  color: #c7d2fe;
  background: rgba(99, 102, 241, 0.18);
  border: 1px solid rgba(99, 102, 241, 0.4);
  padding: 6px 10px; border-radius: 999px;
}

/* ---------- Buttons ---------- */
.btn {
  font: inherit; font-weight: 600; font-size: 14px;
  border: 1px solid transparent;
  border-radius: 11px;
  padding: 10px 16px;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s, border-color 0.15s;
  color: var(--text);
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--muted); }
.btn-ghost:hover { color: var(--text); border-color: #3a4150; }
.btn-soft { background: var(--bg-elev-2); border-color: var(--line); width: 100%; text-align: left; }
.btn-soft:hover { border-color: #3a4150; }
.btn-soft.danger { color: #f3a0a2; }
.btn-soft.danger:hover { border-color: var(--danger); }
.btn.full { width: 100%; }

/* ---------- Layout ---------- */
.layout {
  max-width: 1180px;
  margin: 8px auto 60px;
  padding: 0 clamp(16px, 4vw, 48px);
  padding-left: max(clamp(16px, 4vw, 48px), env(safe-area-inset-left));
  padding-right: max(clamp(16px, 4vw, 48px), env(safe-area-inset-right));
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 22px;
  align-items: start;
}
@media (max-width: 880px) {
  .layout { grid-template-columns: 1fr; }
}

.calendar-card, .legend-card {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.calendar-card { padding: 20px clamp(14px, 2vw, 24px) 24px; }
.legend-card { padding: 20px; position: sticky; top: 16px; }

/* ---------- Calendar header ---------- */
.cal-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 18px;
}
.cal-title { display: flex; align-items: center; gap: 14px; }
.cal-title h2 { margin: 0; font-size: 22px; font-weight: 700; letter-spacing: -0.02em; min-width: 190px; }
.nav-btn {
  width: 40px; height: 40px;
  border-radius: 11px;
  border: 1px solid var(--line);
  background: var(--bg-elev-2);
  color: var(--text);
  font-size: 22px; line-height: 1;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.nav-btn:hover { border-color: #3a4150; background: #232834; }

/* ---------- Grid ---------- */
.weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 8px;
  margin-bottom: 8px;
}
.weekdays span {
  text-align: center;
  font-size: 12px; font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 8px;
}
.cell {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg-elev-2);
  padding: 8px;
  display: flex;
  flex-direction: column;
  cursor: default;
  overflow: hidden;
  transition: transform 0.08s, box-shadow 0.15s, border-color 0.15s;
}
.cell.empty { background: transparent; border-color: transparent; }
.cell.clickable { cursor: pointer; }
.cell.clickable:hover { transform: translateY(-2px); box-shadow: 0 8px 20px rgba(0,0,0,0.35); }
.cell .daynum {
  font-size: 14px; font-weight: 600;
  color: var(--text);
  z-index: 1;
}
.cell.today { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); }
.cell.today .daynum {
  background: var(--primary); color: #fff;
  width: 24px; height: 24px; border-radius: 999px;
  display: grid; place-items: center;
  margin: -2px -2px 0 -2px;
}
.cell .fill {
  position: absolute; inset: 0;
  opacity: 0.92;
}
/* keep the day number legible over any fill colour(s) */
.cell.has-type .daynum {
  align-self: flex-start;
  color: #fff;
  background: rgba(12, 15, 21, 0.55);
  padding: 2px 7px; border-radius: 999px;
  backdrop-filter: blur(2px);
}
.cell.today.has-type .daynum { background: var(--primary); }
.cell .cell-foot {
  z-index: 1; margin-top: auto;
  display: flex; align-items: flex-end; justify-content: flex-end;
}
.cell .note-dot {
  font-size: 13px; line-height: 1; flex-shrink: 0;
  filter: drop-shadow(0 1px 2px rgba(0,0,0,0.5));
}
.cell .type-label {
  font-size: 10.5px; font-weight: 600;
  color: #fff;
  line-height: 1.15;
  background: rgba(12, 15, 21, 0.5);
  padding: 2px 5px; border-radius: 6px;
  backdrop-filter: blur(2px);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden;
}
@media (max-width: 560px) {
  .cell .type-label { display: none; }
  .cell { padding: 6px; }
}
.label-hint { font-weight: 400; color: var(--muted); }

/* ---------- Legend ---------- */
.legend-card h3 { margin: 0 0 14px; font-size: 15px; }
.legend { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.legend li { display: flex; align-items: center; gap: 10px; font-size: 13.5px; }
.legend .swatch {
  width: 18px; height: 18px; border-radius: 6px;
  border: 1px solid rgba(255,255,255,0.2);
  flex-shrink: 0;
}
.admin-tools { margin-top: 18px; display: flex; flex-direction: column; gap: 8px; }
.admin-tools hr { border: none; border-top: 1px solid var(--line); margin: 4px 0 12px; }
.hint { margin: 16px 0 0; font-size: 12.5px; color: var(--muted); line-height: 1.5; }

/* ---------- Modals ---------- */
.modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; }
.modal[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(5, 7, 12, 0.6); backdrop-filter: blur(4px); }
.modal-panel {
  position: relative;
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  max-height: calc(100dvh - 48px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 24px;
  animation: pop 0.16s ease-out;
}
.modal-panel.narrow { width: min(400px, calc(100vw - 32px)); }
@keyframes pop { from { opacity: 0; transform: scale(0.97) translateY(6px); } to { opacity: 1; transform: none; } }
.modal-panel h3 { margin: 0 0 6px; font-size: 18px; }
.modal-close {
  position: absolute; top: 14px; right: 14px;
  width: 32px; height: 32px; border-radius: 9px;
  border: 1px solid var(--line); background: var(--bg-elev-2);
  color: var(--muted); font-size: 20px; cursor: pointer; line-height: 1;
}
.modal-close:hover { color: var(--text); }
.muted { color: var(--muted); font-size: 13px; margin: 0 0 16px; }

.field { margin: 16px 0; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 7px; color: var(--muted); }
input[type="text"], input[type="password"], textarea {
  width: 100%; font: inherit; color: var(--text);
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 11px 13px;
  outline: none;
  transition: border-color 0.15s;
}
input:focus, textarea:focus { border-color: var(--primary); }
textarea { resize: vertical; }

.modal-actions { display: flex; gap: 10px; margin-top: 20px; }

/* ---------- Type choices ---------- */
.type-choices { display: flex; flex-direction: column; gap: 8px; }
.type-choice {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-elev-2);
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s;
}
.type-choice:hover { border-color: #3a4150; }
.type-choice.selected { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); }
.type-choice .swatch { width: 20px; height: 20px; border-radius: 7px; border: 1px solid rgba(255,255,255,0.2); flex-shrink: 0; }
.type-choice .name { font-size: 14px; font-weight: 500; }
.type-choice .check { margin-left: auto; color: var(--primary); font-weight: 700; opacity: 0; }
.type-choice.selected .check { opacity: 1; }

/* ---------- Types management ---------- */
.types-list { list-style: none; margin: 0 0 16px; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.type-row { display: flex; align-items: center; gap: 10px; }
.type-row input[type="color"] {
  width: 42px; height: 42px; padding: 0; border: 1px solid var(--line);
  border-radius: 10px; background: var(--bg-elev-2); cursor: pointer;
}
.type-row input[type="text"] { flex: 1; }
.icon-btn {
  width: 42px; height: 42px; flex-shrink: 0;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg-elev-2); color: var(--muted);
  cursor: pointer; font-size: 16px;
}
.icon-btn:hover { color: var(--text); border-color: #3a4150; }
.icon-btn.del:hover { color: var(--danger); border-color: var(--danger); }
.icon-btn.save { color: #22c55e; }
.add-type { display: flex; gap: 10px; align-items: center; border-top: 1px solid var(--line); padding-top: 16px; }
.add-type input[type="text"] { flex: 1; }
.add-type input[type="color"] { width: 42px; height: 42px; padding: 0; border: 1px solid var(--line); border-radius: 10px; background: var(--bg-elev-2); cursor: pointer; }

.error { color: #f3a0a2; font-size: 13px; margin: 8px 0 0; }
.success { color: #86efac; font-size: 13px; margin: 8px 0 0; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--bg-elev-2); border: 1px solid var(--line);
  color: var(--text); padding: 12px 18px; border-radius: 12px;
  box-shadow: var(--shadow); font-size: 14px; z-index: 80;
  animation: pop 0.16s ease-out;
}
.toast[hidden] { display: none; }

/* ---------- Stats ---------- */
.stats-panel { width: min(940px, calc(100vw - 32px)); padding: 26px; }
.stats-head h2 { margin: 0 0 4px; font-size: 24px; letter-spacing: -0.02em; }
.stats-head .muted { margin: 0 0 8px; }
.stats-body { margin-top: 12px; }

.kpi-grid {
  display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px;
  margin-bottom: 8px;
}
@media (max-width: 760px) { .kpi-grid { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 440px) { .kpi-grid { grid-template-columns: repeat(2, 1fr); } }
.kpi {
  background: linear-gradient(160deg, var(--bg-elev-2), rgba(99,102,241,0.06));
  border: 1px solid var(--line);
  border-radius: 14px; padding: 14px 12px; text-align: center;
}
.kpi-val { font-size: 26px; font-weight: 800; letter-spacing: -0.03em; line-height: 1; }
.kpi-label { font-size: 12px; font-weight: 600; color: var(--text); margin-top: 6px; }
.kpi-sub { font-size: 11px; color: var(--muted); margin-top: 2px; }

.stats-section { margin-top: 26px; }
.stats-section h3 { margin: 0 0 14px; font-size: 16px; }
.stat-note { font-size: 12.5px; color: var(--muted); margin: 12px 0 0; }
.stat-note strong { color: var(--text); }

.bars { display: flex; flex-direction: column; gap: 9px; }
.bar-row { display: grid; grid-template-columns: 160px 1fr 36px; align-items: center; gap: 12px; }
@media (max-width: 560px) { .bar-row { grid-template-columns: 110px 1fr 30px; } }
.bar-label { font-size: 13px; display: flex; align-items: center; gap: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-swatch { width: 13px; height: 13px; border-radius: 4px; border: 1px solid rgba(255,255,255,0.2); flex-shrink: 0; }
.bar-track { background: var(--bg-elev-2); border-radius: 999px; height: 12px; overflow: hidden; }
.bar-fill { display: block; height: 100%; border-radius: 999px; transition: width 0.5s ease; min-width: 3px; }
.bar-val { font-size: 13px; font-weight: 700; text-align: right; }

.person-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.person-card {
  background: var(--bg-elev-2); border: 1px solid var(--line);
  border-radius: 16px; padding: 18px 12px; text-align: center;
}
.person-emoji { font-size: 30px; }
.person-count { font-size: 32px; font-weight: 800; letter-spacing: -0.03em; margin-top: 4px; }
.person-name { font-size: 14px; font-weight: 600; color: var(--muted); }
.person-bar { height: 6px; background: var(--bg-elev); border-radius: 999px; margin-top: 12px; overflow: hidden; }
.person-bar span { display: block; height: 100%; background: linear-gradient(90deg, var(--primary), #22c55e); border-radius: 999px; transition: width 0.5s ease; }

.col-chart {
  display: flex; gap: 6px; align-items: flex-end;
  height: 160px; overflow-x: auto; padding-bottom: 4px;
}
.col { flex: 1 0 38px; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; }
.col-val { font-size: 11px; font-weight: 700; color: var(--muted); margin-bottom: 4px; }
.col-bar {
  width: 70%; min-height: 0;
  background: linear-gradient(180deg, var(--primary), #4f46e5);
  border-radius: 6px 6px 0 0; transition: height 0.5s ease;
}
.col-label { font-size: 10px; color: var(--muted); margin-top: 6px; text-align: center; line-height: 1.1; }

.heatmap {
  display: grid; grid-template-rows: repeat(7, 1fr); grid-auto-flow: column; grid-auto-columns: 1fr;
  gap: 4px; max-width: 100%;
}
.hcell { aspect-ratio: 1/1; border-radius: 3px; background: var(--bg-elev-2); border: 1px solid var(--line); }
.hcell.on { border-color: rgba(255,255,255,0.18); }
.hcell.future { opacity: 0; }

/* ==================== Phone optimisations ==================== */
@media (max-width: 600px) {
  /* compact topbar that fits next to the action buttons */
  .topbar { padding-top: max(12px, env(safe-area-inset-top)); padding-bottom: 12px; gap: 8px; }
  .brand { gap: 10px; }
  .brand-mark { width: 38px; height: 38px; font-size: 18px; border-radius: 11px; }
  .brand-text h1 { font-size: 16px; }
  .brand-text p { display: none; }              /* subtitle is redundant on small screens */
  .topbar-actions { gap: 8px; }
  .btn { padding: 9px 13px; font-size: 13.5px; }
  .hide-sm { display: none; }                    /* "📊 Treffen Stats" -> "📊 Stats" */
}

@media (max-width: 560px) {
  .layout { gap: 14px; margin: 6px auto 32px; }
  .calendar-card { padding: 14px 12px 16px; border-radius: 14px; }
  .legend-card { padding: 16px; position: static; }
  .cal-header { margin-bottom: 14px; }
  .cal-title { gap: 8px; }
  .cal-title h2 { font-size: 19px; min-width: 0; }
  .nav-btn { width: 38px; height: 38px; }
  #todayBtn { padding: 8px 11px; }

  .grid, .weekdays { gap: 5px; }
  .cell { padding: 5px; border-radius: 10px; }
  .cell.clickable:active { transform: scale(0.96); }   /* touch feedback */
  .cell.clickable:hover { transform: none; box-shadow: none; }  /* no sticky hover on touch */
  .cell .daynum { font-size: 13px; }
  .cell.has-type .daynum { padding: 1px 6px; }
  .cell .note-dot { font-size: 12px; }
  .weekdays span { font-size: 10.5px; }
}

/* Bottom-sheet style modals on phones */
@media (max-width: 560px) {
  .modal { place-items: end stretch; }
  .modal-panel,
  .modal-panel.narrow,
  .stats-panel {
    width: 100%;
    max-width: 100%;
    max-height: 90dvh;
    border-radius: 22px 22px 0 0;
    padding: 22px 18px;
    padding-bottom: max(22px, calc(env(safe-area-inset-bottom) + 12px));
    animation: slideUp 0.24s cubic-bezier(0.22, 1, 0.36, 1);
  }
  /* grab handle */
  .modal-panel::before {
    content: ""; display: block;
    width: 40px; height: 4px; margin: -8px auto 14px;
    background: var(--line); border-radius: 999px;
  }
  .modal-close { top: 14px; right: 14px; }
  .stats-head h2 { font-size: 21px; }

  .kpi-grid { gap: 10px; }
  .kpi { padding: 13px 10px; }
  .kpi-val { font-size: 22px; }

  .bar-row { grid-template-columns: 96px 1fr 26px; gap: 9px; }
  .bar-label { font-size: 12px; }
  .person-grid { gap: 10px; }
  .person-card { padding: 14px 8px; }
  .person-emoji { font-size: 26px; }
  .person-count { font-size: 26px; }

  .modal-actions .btn { flex: 1; padding: 13px; }   /* bigger, even tap targets */
}

@keyframes slideUp { from { transform: translateY(100%); } to { transform: none; } }

/* devices without hover (touch): no sticky hover lift on cells */
@media (hover: none) {
  .cell.clickable:hover { transform: none; box-shadow: 0 0 0 1px var(--line); }
}

/* ---------- Day details (read-only) ---------- */
.view-types { display: flex; flex-direction: column; gap: 8px; margin: 14px 0; }
.view-type {
  display: flex; align-items: center; gap: 11px;
  font-size: 14px; font-weight: 500;
  background: var(--bg-elev-2); border: 1px solid var(--line);
  border-radius: 11px; padding: 11px 13px;
}
.view-type .swatch {
  width: 18px; height: 18px; border-radius: 6px;
  border: 1px solid rgba(255,255,255,0.2); flex-shrink: 0;
}
.view-note { margin-top: 6px; }
.view-note-label { font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 7px; }
.note-box {
  font-size: 15px; line-height: 1.55; white-space: pre-wrap; word-break: break-word;
  background: var(--bg-elev-2); border: 1px solid var(--line);
  border-radius: 12px; padding: 13px 14px;
}
