:root {
  --bg: #0b0c0e;
  --surface: #131519;
  --surface-2: #1a1d22;
  --line: #24282f;
  --line-strong: #323843;
  --text: #eceef1;
  --muted: #7d838e;
  --accent: #c8f53a;
  --accent-ink: #11140a;
  --ok: #3ddc84;
  --danger: #ff5c63;
  --warn: #ffb547;

  --font-display: "Syne", system-ui, sans-serif;
  --font-mono: "DM Mono", ui-monospace, monospace;

  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);

  --bar-h: 48px;
  --pad-x: 20px;
  --pad-y: 10px;
  --gap: 14px;

  /* Nutzbare Fläche einer Seite */
  --page-w: calc(100vw - var(--safe-l) - var(--safe-r) - 2 * var(--pad-x));
  --page-h: calc(100dvh - var(--bar-h) - var(--safe-t) - var(--safe-b) - 2 * var(--pad-y));

  /* Kachelgröße: passt immer exakt in 4 × 2 bzw. 5 × 2, quadratisch */
  --app-t: min(calc((var(--page-w) - 3 * var(--gap)) / 4), calc((var(--page-h) - var(--gap)) / 2), 210px);
  --ctl-t: min(calc((var(--page-w) - 4 * var(--gap)) / 5), calc((var(--page-h) - var(--gap)) / 2), 170px);
}

*,
*::before,
*::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; }

svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
}

/* ---------- Kopfzeile ---------- */

.bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  flex: none;
  height: var(--bar-h);
  padding: 0 var(--pad-x);
}

.brand {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 20px;
  letter-spacing: -.01em;
}

.dots { display: flex; gap: 4px; }

.dot {
  width: 22px;
  height: 22px;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
}

.dot::before {
  content: "";
  display: block;
  width: 7px;
  height: 7px;
  margin: auto;
  border-radius: 50%;
  background: var(--line-strong);
  transition: background-color .2s ease, transform .2s ease;
}

.dot[aria-current="true"]::before { background: var(--text); transform: scale(1.15); }

.bar-right {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
}

/* Feste Breiten → kein Layout-Shift bei Status- oder Moduswechsel */
.status,
.edit-btn {
  display: inline-flex;
  align-items: center;
  height: 30px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 12px;
  white-space: nowrap;
}

.status {
  gap: 7px;
  width: 104px;
  padding: 0 11px;
  color: var(--muted);
}

.status-dot {
  flex: none;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
}

.status[data-state="online"] .status-dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 20%, transparent); }
.status[data-state="online"] .status-text { color: var(--text); }
.status[data-state="offline"] .status-dot { background: var(--danger); }
.status[data-state="error"] .status-dot { background: var(--warn); }

.edit-btn {
  justify-content: center;
  width: 96px;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}

.edit-btn[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

/* ---------- Seiten ---------- */

.pager {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  scrollbar-width: none;
}

.pager::-webkit-scrollbar { display: none; }

.app.editing .pager { overflow-x: hidden; }

.page {
  display: grid;
  place-items: center;
  flex: 0 0 100%;
  padding: var(--pad-y) var(--pad-x);
  scroll-snap-align: start;
  scroll-snap-stop: always;
}

.apps {
  display: grid;
  grid-template-columns: repeat(4, var(--app-t));
  grid-template-rows: repeat(2, var(--app-t));
  gap: var(--gap);
}

.controls {
  display: grid;
  grid-template-columns: repeat(5, var(--ctl-t));
  grid-template-rows: repeat(2, var(--ctl-t));
  gap: var(--gap);
}

/* ---------- App-Kacheln ---------- */

.slot {
  position: relative;
  display: grid;
  place-items: center;
  width: var(--app-t);
  height: var(--app-t);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: calc(var(--app-t) * .24);
  background: var(--surface);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform .12s ease, background-color .15s ease, border-color .15s ease, opacity .15s ease;
}

.slot img {
  width: 60%;
  height: 60%;
  object-fit: contain;
  pointer-events: none;
  -webkit-user-drag: none;
}

/* Platzhalter, solange der Agent noch kein Logo geliefert hat */
.monogram {
  display: grid;
  place-items: center;
  gap: 4px;
  pointer-events: none;
}

.monogram b {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: calc(var(--app-t) * .3);
  line-height: 1;
}

.monogram small {
  max-width: calc(var(--app-t) * .84);
  overflow: hidden;
  color: var(--muted);
  font-size: clamp(10px, calc(var(--app-t) * .085), 13px);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.slot.empty {
  border-style: dashed;
  background: transparent;
  cursor: default;
}

.slot.empty svg { width: 30%; height: 30%; color: var(--line-strong); opacity: 0; }

@media (hover: hover) {
  .slot:not(.empty):hover { background: var(--surface-2); border-color: var(--line-strong); }
}

.slot:not(.empty):active { transform: scale(.95); }

.slot:focus-visible,
.tile:focus-visible,
.edit-btn:focus-visible,
.dot:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.slot[data-state="busy"]::after,
.tile[data-state="busy"]::after {
  content: "";
  position: absolute;
  top: 12%;
  right: 12%;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse .8s ease-in-out infinite;
}

.slot[data-state="ok"],
.tile[data-state="ok"] { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }

.slot[data-state="fail"],
.tile[data-state="fail"] { border-color: var(--danger); box-shadow: 0 0 0 2px var(--danger); }

@keyframes pulse { 50% { opacity: .25; } }

/* Bearbeiten-Modus */
.app.editing .slot {
  cursor: grab;
  touch-action: none;
  border-color: var(--line-strong);
  animation: jiggle .32s ease-in-out infinite alternate;
}

.app.editing .slot:nth-child(2n) { animation-delay: -.16s; }
.app.editing .slot.empty { cursor: pointer; animation: none; }
.app.editing .slot.empty svg { opacity: 1; }
.app.editing .slot.dragging { opacity: .25; animation: none; }
.app.editing .slot.drop-target { border-color: var(--accent); background: var(--surface-2); animation: none; }

@keyframes jiggle {
  from { transform: rotate(-1deg); }
  to { transform: rotate(1deg); }
}

.drag-ghost {
  position: fixed;
  z-index: 50;
  margin: 0;
  pointer-events: none;
  opacity: .92;
  transform: scale(1.06);
  box-shadow: 0 16px 40px rgba(0, 0, 0, .55);
  animation: none !important;
}

/* ---------- Steuerungs-Kacheln (Seite 2) ---------- */

.tile {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: var(--ctl-t);
  height: var(--ctl-t);
  padding: calc(var(--ctl-t) * .13);
  border: 1px solid var(--line);
  border-radius: calc(var(--ctl-t) * .2);
  background: var(--surface);
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform .12s ease, background-color .15s ease, border-color .15s ease;
}

.tile svg { width: calc(var(--ctl-t) * .24); height: calc(var(--ctl-t) * .24); }

.tile-label {
  overflow: hidden;
  font-size: clamp(10px, calc(var(--ctl-t) * .1), 13px);
  line-height: 1.2;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (hover: hover) {
  .tile:hover { background: var(--surface-2); border-color: var(--line-strong); }
}

.tile:active { transform: scale(.95); }

/* ---------- Auswahl-Dialog ---------- */

.picker {
  width: min(640px, calc(100vw - var(--safe-l) - var(--safe-r) - 32px));
  max-height: calc(100dvh - var(--safe-t) - var(--safe-b) - 32px);
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 20px;
  background: var(--surface);
  color: var(--text);
}

.picker::backdrop { background: rgba(5, 6, 8, .72); }

.picker-inner { padding: 14px 16px 16px; }

.picker-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.picker-head h2 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 17px;
}

.picker-close {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface-2);
  cursor: pointer;
}

.picker-close svg { width: 16px; height: 16px; }

.picker-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.choice {
  position: relative;
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 6px;
  height: 92px;
  padding: 8px 6px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface-2);
  cursor: pointer;
}

.choice img,
.choice .choice-mono,
.choice > svg {
  width: 38px;
  height: 38px;
}

.choice img { object-fit: contain; }

.choice .choice-mono {
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: var(--bg);
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 18px;
}

.choice > svg { padding: 8px; color: var(--muted); }

.choice-name {
  max-width: 100%;
  overflow: hidden;
  font-size: 11px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.choice-where {
  position: absolute;
  top: 6px;
  right: 8px;
  color: var(--muted);
  font-size: 10px;
}

.choice[aria-current="true"] { border-color: var(--accent); }

@media (hover: hover) {
  .choice:hover { border-color: var(--line-strong); }
}

/* ---------- Hochformat-Hinweis ---------- */

.rotate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: var(--bg);
  color: var(--muted);
  font-size: 13px;
}

.rotate svg { width: 44px; height: 44px; color: var(--text); }

@media (orientation: portrait) and (max-width: 700px) {
  .rotate { display: flex; }
}

/* ---------- Abmelden (nur im Bearbeiten-Modus, ersetzt den Status) ---------- */

.logout-btn {
  display: none;
  justify-content: center;
  align-items: center;
  width: 104px;
  height: 30px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--danger);
  font-size: 12px;
  cursor: pointer;
  touch-action: manipulation;
}

.app.editing .status { display: none; }
.app.editing .logout-btn { display: inline-flex; }

/* ---------- Login ---------- */

.login-body {
  display: grid;
  place-items: center;
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
  user-select: auto;
  -webkit-user-select: auto;
}

.login {
  display: flex;
  flex-direction: column;
  width: min(340px, calc(100vw - 32px));
}

.brand-kicker {
  font-size: 11px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--muted);
}

.login-title {
  margin: 2px 0 20px;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 40px;
  line-height: 1;
  letter-spacing: -.02em;
}

.login-form {
  display: flex;
  gap: 8px;
}

.login-input,
.login-btn {
  height: 46px;
  border-radius: 14px;
  font: inherit;
  font-size: 16px; /* < 16px würde iOS beim Fokus hineinzoomen */
}

.login-input {
  flex: 1;
  min-width: 0;
  padding: 0 14px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--text);
  outline: none;
}

.login-input:focus { border-color: var(--accent); }

.login-btn {
  flex: none;
  padding: 0 16px;
  border: 0;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}

/* Feste Höhe → kein Springen, wenn eine Meldung erscheint */
.login-error {
  height: 18px;
  margin: 12px 0 0;
  color: var(--danger);
  font-size: 12px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-b) + 14px);
  z-index: 60;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  background: var(--surface-2);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 8px);
  transition: opacity .18s ease, transform .18s ease;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast[data-kind="error"] { border-color: color-mix(in srgb, var(--danger) 55%, var(--line)); }

/* ---------- Größere Bildschirme ---------- */

@media (min-width: 1000px) and (min-height: 600px) {
  :root { --bar-h: 64px; --pad-x: 40px; --pad-y: 24px; --gap: 20px; }
  .brand { font-size: 24px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
