/* Themes live entirely in these custom properties -- no component below reaches
   for a literal colour, so a theme is a swap of this block and nothing else.

   Three layers, in this order:
     :root                              -> light, the default
     @media (prefers-color-scheme:dark) -> follow the OS unless overridden
     [data-theme="..."]                 -> the explicit choice always wins
   The data-theme attribute is set by app.js before first paint. */
:root {
  --bg: #f5f6fa;
  --card-bg: #ffffff;
  --border: #e2e5ec;
  --text: #1c2233;
  --muted: #6b7280;
  --primary: #2f6feb;
  --primary-dark: #2456bd;
  --on-primary: #ffffff;
  --danger: #d64545;
  --danger-bg: #fdecec;
  --danger-border: #f3c2c2;
  --danger-text: #992222;
  --ok: #2e9e5b;
  --ok-bg: #e9f7ef;
  --ok-border: #bfe5cd;
  --ok-text: #1d6b3c;
  --warn: #d98324;
  --warn-bg: #fdf3e7;
  --warn-border: #f0d5ae;
  --warn-text: #8a5312;
  --off: #9aa1ad;
  --radius: 10px;
}

/* Dark values are not the light ones inverted: surfaces stay slightly lighter
   than the page so cards keep their depth, and the accent is lifted because
   #2f6feb on a dark background falls below a readable contrast. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151c;
    --card-bg: #1b1f29;
    --border: #2c313d;
    --text: #e6e9ef;
    --muted: #9aa3b2;
    --primary: #6a9bff;
    --primary-dark: #86b0ff;
    --on-primary: #0d1017;
    --danger: #ff8080;
    --danger-bg: #2e1c1e;
    --danger-border: #56302f;
    --danger-text: #ffb3b3;
    --ok: #5cc98a;
    --ok-bg: #16281e;
    --ok-border: #2c4a37;
    --ok-text: #9fe0b8;
    --warn: #f0a850;
    --warn-bg: #2b2113;
    --warn-border: #4d3b1e;
    --warn-text: #f3c98a;
    --off: #6b7385;
  }
}

:root[data-theme="dark"] {
  --bg: #12151c;
  --card-bg: #1b1f29;
  --border: #2c313d;
  --text: #e6e9ef;
  --muted: #9aa3b2;
  --primary: #6a9bff;
  --primary-dark: #86b0ff;
  --on-primary: #0d1017;
  --danger: #ff8080;
  --danger-bg: #2e1c1e;
  --danger-border: #56302f;
  --danger-text: #ffb3b3;
  --ok: #5cc98a;
  --ok-bg: #16281e;
  --ok-border: #2c4a37;
  --ok-text: #9fe0b8;
  --warn: #f0a850;
  --warn-bg: #2b2113;
  --warn-border: #4d3b1e;
  --warn-text: #f3c98a;
  --off: #6b7385;
}

/* Tells the browser to render form controls, scrollbars and the caret dark, so
   a <select> or the datetime picker does not stay glaringly white. */
:root {
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
}
:root[data-theme="light"] {
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  /* Room for the fixed bottom nav on phones. */
  padding-bottom: 4.5rem;
}

a {
  color: var(--primary);
}

/* --- Topbar --- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  font-weight: 700;
  font-size: 1.15rem;
  color: var(--primary);
  text-decoration: none;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.theme-toggle {
  /* Fixed width so cycling Auto/Hell/Dunkel does not shift the topbar. */
  min-width: 4.6rem;
}

/* --- Layout --- */
main {
  max-width: 900px;
  margin: 0 auto;
  padding: 1rem;
}

h1 {
  font-size: 1.4rem;
  margin: 0.5rem 0 1rem;
}

h2 {
  font-size: 1.1rem;
  margin: 0 0 0.75rem;
}

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
}

.row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.spread {
  justify-content: space-between;
}

.muted {
  color: var(--muted);
  font-size: 0.85rem;
}

/* --- Forms --- */
label {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

input,
select,
textarea {
  width: 100%;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  font-family: inherit;
  background: var(--card-bg);
  color: var(--text);
}

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
}

.field {
  margin-bottom: 0.85rem;
}

.field-row {
  display: flex;
  gap: 0.75rem;
}

.field-row > * {
  flex: 1;
}

/* --- Buttons --- */
button,
.btn {
  display: inline-block;
  padding: 0.55rem 0.9rem;
  border: 1px solid transparent;
  border-radius: 8px;
  background: var(--primary);
  color: var(--on-primary);
  font-size: 0.95rem;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
}

button:hover,
.btn:hover {
  background: var(--primary-dark);
}

.btn-secondary {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}

.btn-secondary:hover {
  background: var(--bg);
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--border);
}

.btn-danger:hover {
  background: var(--danger-bg);
}

.btn-sm {
  padding: 0.3rem 0.6rem;
  font-size: 0.8rem;
}

.btn-block {
  display: block;
  width: 100%;
}

/* --- Tables --- */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

th,
td {
  text-align: left;
  padding: 0.5rem 0.4rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

tr:last-child td {
  border-bottom: none;
}

.table-scroll {
  overflow-x: auto;
}

/* --- Alerts --- */
.alert {
  padding: 0.6rem 0.8rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-size: 0.9rem;
  border: 1px solid;
}

.alert-error {
  background: var(--danger-bg);
  border-color: var(--danger-border);
  color: var(--danger-text);
}

.alert-ok {
  background: var(--ok-bg);
  border-color: var(--ok-border);
  color: var(--ok-text);
}

.alert-warn {
  background: var(--warn-bg);
  border-color: var(--warn-border);
  color: var(--warn-text);
}

/* --- Badges --- */
.badge {
  display: inline-block;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  background: var(--bg);
  color: var(--muted);
  border: 1px solid var(--border);
  /* Without this a label like "noch 5 h 59 min" wraps to four lines inside a
     narrow table column on a phone. */
  white-space: nowrap;
}

.badge-ok {
  background: var(--ok-bg);
  color: var(--ok);
  border-color: var(--ok-border);
}

.badge-due {
  background: var(--warn-bg);
  color: var(--warn);
  border-color: var(--warn-border);
}

.badge-over {
  background: var(--danger-bg);
  color: var(--danger);
  border-color: var(--danger-border);
}

.badge-off {
  color: var(--off);
}

/* --- Utilization meter (the JS-free fallback for the chart) --- */
.meter {
  height: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 0.3rem;
}

.meter-fill {
  height: 100%;
  background: var(--primary);
}

.meter-fill.over {
  background: var(--danger);
}

.meter-fill.high {
  background: var(--warn);
}

.stat {
  font-size: 1.5rem;
  font-weight: 700;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 0.75rem;
}

/* --- Quick log --- */
.quick-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 0.6rem;
}

.quick-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.15rem;
  padding: 0.7rem 0.8rem;
  text-align: left;
}

.quick-btn small {
  font-weight: 400;
  opacity: 0.85;
  font-size: 0.75rem;
}

/* --- Chart --- */
.chart-wrap {
  position: relative;
  height: 300px;
}

.chart-controls {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
}

.chart-controls button {
  background: transparent;
  color: var(--muted);
  border-color: var(--border);
  font-weight: 500;
}

.chart-controls button[aria-pressed="true"] {
  background: var(--primary);
  color: var(--on-primary);
  border-color: var(--primary);
  font-weight: 600;
}

/* --- Bottom nav --- */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--card-bg);
  border-top: 1px solid var(--border);
  z-index: 10;
  padding-bottom: env(safe-area-inset-bottom);
}

.bottom-nav a {
  flex: 1;
  text-align: center;
  padding: 0.7rem 0.25rem;
  font-size: 0.75rem;
  color: var(--muted);
  text-decoration: none;
}

.bottom-nav a.active {
  color: var(--primary);
  font-weight: 600;
}

/* --- Auth pages --- */
.auth-wrap {
  max-width: 380px;
  margin: 3rem auto;
  padding: 0 1rem;
}

.auth-wrap .brand {
  display: block;
  text-align: center;
  font-size: 1.6rem;
  margin-bottom: 1.5rem;
}

/* --- Misc --- */
.empty {
  text-align: center;
  color: var(--muted);
  padding: 1.5rem 0;
  font-size: 0.9rem;
}

.hidden {
  display: none !important;
}

.disclaimer {
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
  margin: 1.5rem 0 0.5rem;
  line-height: 1.4;
}

/* Long free-text notes must not stretch the table into a horizontal scroll. */
.note-cell {
  max-width: 20rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

@media (max-width: 480px) {
  .field-row {
    flex-direction: column;
    gap: 0;
  }

  /* The topbar has to fit the theme switch on a phone; the email is the least
     useful thing there and it is on the settings page anyway. */
  .hide-narrow {
    display: none;
  }
}
