:root {
  --bg: #0b1220;
  --bg-2: #111b2e;
  --bg-3: #18243c;
  --line: #23324e;
  --txt: #e7edf7;
  --muted: #8ea0bd;
  --accent: #38bdf8;
  --accent-2: #0ea5e9;
  --ok: #34d399;
  --warn: #fbbf24;
  --bad: #f87171;
  --radius: 12px;
  --tap: 44px;                 /* Mindestgröße fuer Touch-Ziele */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--txt);
  font-size: 14px;
  overflow-x: hidden;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--bad); min-height: 18px; margin: 6px 0 0; }
.ok { color: var(--ok); }

/* ---------------------------------------------------------- Login */
.login-wrap {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
  background: radial-gradient(1100px 600px at 15% -10%, #16324f 0%, var(--bg) 55%);
}
.login-card {
  width: min(400px, 100%);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px;
  display: grid;
  gap: 14px;
  box-shadow: 0 24px 60px rgba(0,0,0,.45);
}
.login-card h1 { margin: 0; font-size: 21px; }
.brand { display: flex; align-items: center; gap: 12px; }
.brand-lg { margin-bottom: 6px; }
.brand p { margin: 2px 0 0; }
.logo {
  display: grid; place-items: center;
  width: 40px; height: 40px; border-radius: 10px;
  background: linear-gradient(140deg, var(--accent), #6366f1);
  color: #04121f; font-weight: 800; letter-spacing: .5px;
  flex: 0 0 auto;
}

/* ---------------------------------------------------------- Layout */
#app-view { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; min-height: 100dvh; }
.sidebar {
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  padding: 18px 14px;
  display: flex; flex-direction: column; gap: 18px;
}
#nav { display: flex; flex-direction: column; gap: 4px; }
.nav-item {
  text-align: left; background: transparent; border: 0; color: var(--muted);
  padding: 9px 12px; border-radius: 9px; cursor: pointer; font-size: 14px;
  display: flex; justify-content: space-between; align-items: center;
}
.nav-item:hover { background: var(--bg-3); color: var(--txt); }
.nav-item.active { background: var(--bg-3); color: var(--txt); box-shadow: inset 2px 0 0 var(--accent); }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 8px; }

.content { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 24px; border-bottom: 1px solid var(--line); background: var(--bg-2);
  gap: 10px;
}
.topbar h2 { margin: 0; font-size: 17px; }
.view { padding: 22px 24px 60px; }

/* Menüknopf und Schleier - nur auf kleinen Bildschirmen sichtbar */
.nav-toggle {
  display: none; background: var(--bg-3); border: 1px solid var(--line);
  color: var(--txt); border-radius: 10px; line-height: 1;
  width: var(--tap); height: var(--tap); cursor: pointer; flex: 0 0 auto;
  align-items: center; justify-content: center; padding: 0;
}
.nav-toggle svg { display: block; }
.nav-overlay {
  display: none; position: fixed; inset: 0; z-index: 55;
  background: rgba(3, 7, 14, .62);
}
.nav-overlay.on { display: block; }

/* ---------------------------------------------------------- Bausteine */
.grid { display: grid; gap: 14px; }
.grid.cols-4 { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.card {
  background: var(--bg-2); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px;
}
.card h3 { margin: 0 0 10px; font-size: 14px; color: var(--muted); font-weight: 600; }
.stat { font-size: 28px; font-weight: 700; }
.stat.ok { color: var(--ok); }
.stat.warn { color: var(--warn); }
.stat.bad { color: var(--bad); }

.btn {
  background: var(--bg-3); color: var(--txt); border: 1px solid var(--line);
  border-radius: 9px; padding: 8px 12px; cursor: pointer; font-size: 13px;
  min-height: 36px;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent-2); border-color: var(--accent-2); color: #04121f; font-weight: 600; }
.btn.danger { background: #3a1620; border-color: #6b2233; color: #ffd7de; }
.btn.small { padding: 5px 9px; font-size: 12px; min-height: 32px; }
.btn.full { width: 100%; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spread { justify-content: space-between; }

input, select, textarea {
  background: var(--bg); border: 1px solid var(--line); color: var(--txt);
  border-radius: 9px; padding: 9px 11px; font-size: 13px; font-family: inherit; width: 100%;
  min-height: 38px;
}
input:focus, select:focus, textarea:focus { outline: 1px solid var(--accent); }
label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
textarea { min-height: 160px; font-family: ui-monospace, "Cascadia Code", monospace; }

/* Tabellen: umbrechen und bei Bedarf seitlich schieben */
.table-wrap {
  width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch;
}
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
tbody tr:hover { background: var(--bg-3); }
td.mono, .mono { font-family: ui-monospace, monospace; font-size: 12px; }

.pill {
  display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 11px;
  border: 1px solid var(--line); background: var(--bg-3);
}
.pill.online { color: var(--ok); border-color: #1d5c46; }
.pill.offline { color: var(--bad); border-color: #6b2233; }
.pill.warn { color: var(--warn); border-color: #6b5320; }
.pill.done { color: var(--ok); border-color: #1d5c46; }
.pill.pending { color: var(--accent); border-color: #1c4a63; }
.pill.failed, .pill.timeout { color: var(--bad); border-color: #6b2233; }

.badge {
  background: var(--bad); color: #180508; border-radius: 999px;
  padding: 1px 7px; font-size: 11px; font-weight: 700;
}

.bar { height: 7px; background: var(--bg-3); border-radius: 999px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--accent); }
.bar > span.warn { background: var(--warn); }
.bar > span.bad { background: var(--bad); }

.empty { padding: 30px; text-align: center; color: var(--muted); }
pre.out {
  background: #060c16; border: 1px solid var(--line); border-radius: 9px;
  padding: 12px; max-height: 320px; overflow: auto; font-size: 12px; white-space: pre-wrap;
  word-break: break-word;
}
code.copy {
  display: block; background: #060c16; border: 1px solid var(--line); border-radius: 9px;
  padding: 12px; font-size: 12px; overflow-x: auto; white-space: pre; cursor: pointer;
}
.shot img { width: 100%; border-radius: 9px; border: 1px solid var(--line); }

/* ---------------------------------------------------------- Fernsteuerung */
.rv { display: flex; flex-direction: column; gap: 10px; }
.rv-bar {
  display: flex; justify-content: space-between; align-items: center;
  gap: 10px; flex-wrap: wrap;
}
.rv-switch { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
.rv-switch input { width: auto; min-height: 0; }
.rv-stage {
  background: #05090f; border: 1px solid var(--line); border-radius: 10px;
  display: grid; place-items: center; min-height: 320px; overflow: hidden;
  position: relative; outline: none;
}
.rv-stage:focus { border-color: var(--accent); }
.rv-stage canvas { display: block; cursor: crosshair; max-width: 100%; }
.rv-stage.steering { touch-action: none; }   /* Wischen steuert statt zu scrollen */
.rv-hint {
  position: absolute; color: var(--muted); font-size: 13px; pointer-events: none;
  text-align: center; padding: 0 16px;
}
.rv-foot { line-height: 1.6; }
.rv-keys { display: flex; gap: 6px; flex-wrap: wrap; }
.rv-keys .btn { min-width: 52px; justify-content: center; }
.rv-keys .btn.armed { background: var(--accent-2); border-color: var(--accent-2); color: #04121f; font-weight: 600; }
.rv-textrow { display: flex; gap: 6px; }
.rv-textrow input { flex: 1 1 auto; min-width: 0; }
.warnbox {
  border: 1px solid #6b5320; background: #2a2210; color: #ffe9b0;
  border-radius: 9px; padding: 10px 12px;
}
.dot { width: 8px; height: 8px; border-radius: 999px; background: transparent; display: inline-block; }
.dot.on { background: var(--bad); box-shadow: 0 0 0 3px rgba(248,113,113,.18); }

/* ---------------------------------------------------------- Modal & Toast */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(4, 9, 18, .72);
  display: grid; place-items: center; z-index: 50; padding: 20px;
  /* minmax(0,1fr) verhindert, dass lange Codeblloeke das Raster aufsprengen */
  grid-template-columns: minmax(0, 1fr);
}
.modal {
  background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius);
  width: min(760px, 96vw); max-height: 88vh; display: flex; flex-direction: column;
  min-width: 0; max-width: 100%; margin: 0 auto; overflow: hidden;
}
.modal header { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); gap: 10px; flex: 0 0 auto; }
.modal header h3 { margin: 0; font-size: 15px; min-width: 0; overflow-wrap: anywhere; }
/* min-height:0 ist noetig, damit der Inhalt intern scrollt statt den Dialog
   ueber den Bildschirm hinaus wachsen zu lassen (Flexbox-Vorgabe). */
.modal-body { padding: 18px; overflow: auto; min-width: 0; min-height: 0; -webkit-overflow-scrolling: touch; }
/* Alles im Dialog darf schrumpfen */
.modal-body *, .modal-body * > * { min-width: 0; }
.modal-body .card { overflow-wrap: anywhere; }
.modal-body ol, .modal-body ul { padding-left: 18px; }
.modal-body li { overflow-wrap: anywhere; }

.toast {
  position: fixed; bottom: calc(22px + var(--safe-bottom)); right: 22px; z-index: 90;
  background: var(--bg-3); border: 1px solid var(--accent); color: var(--txt);
  padding: 12px 16px; border-radius: 10px; max-width: 420px; font-size: 13px;
  box-shadow: 0 12px 30px rgba(0,0,0,.4);
}
.toast.bad { border-color: var(--bad); }

/* ==========================================================================
   Tablet / Handy
   ========================================================================== */
@media (max-width: 900px) {
  #app-view { grid-template-columns: 1fr; }

  /* Seitenleiste wird ein ausklappbares Menue */
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; width: min(280px, 82vw);
    transform: translateX(-102%); transition: transform .22s ease;
    z-index: 60; overflow-y: auto; border-right: 1px solid var(--line);
    padding: calc(16px + var(--safe-top)) 14px calc(16px + var(--safe-bottom));
  }
  .sidebar.open { transform: none; box-shadow: 24px 0 60px rgba(0,0,0,.5); }
  #nav { flex-direction: column; }
  .sidebar-foot { margin-top: 18px; }
  .nav-item { min-height: var(--tap); font-size: 15px; }

  .nav-toggle { display: flex; }
  .topbar {
    position: sticky; top: 0; z-index: 40;
    padding: 10px calc(14px + var(--safe-right)) 10px calc(14px + var(--safe-left));
  }
  .topbar h2 { font-size: 16px; }
  .view {
    padding: 14px calc(14px + var(--safe-right)) calc(40px + var(--safe-bottom))
             calc(14px + var(--safe-left));
  }

  /* Bequemer tippbare Elemente */
  .btn { min-height: var(--tap); padding: 10px 14px; font-size: 14px; }
  .btn.small { min-height: 38px; padding: 8px 12px; font-size: 13px; }
  /* 16px verhindert das automatische Hineinzoomen auf iOS */
  input, select, textarea { font-size: 16px; min-height: var(--tap); }
  label { font-size: 14px; }

  .grid.cols-4 { grid-template-columns: repeat(auto-fit, minmax(144px, 1fr)); gap: 10px; }
  .grid.cols-2 { grid-template-columns: 1fr; }
  .card { padding: 14px; }
  .stat { font-size: 24px; }

  /* Dialoge nehmen den ganzen Bildschirm ein */
  .modal-backdrop { padding: 0; padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left); }
  .modal { width: 100%; height: 100%; max-height: 100%; border-radius: 0; border: 0; margin: 0; }
  .modal header { position: sticky; top: 0; background: var(--bg-2); z-index: 2; padding: 12px 14px; }
  .modal header h3 { font-size: 16px; }
  .modal-body { padding: 14px; }
  /* Lange Befehle/Code umbrechen statt seitlich scrollen */
  code.copy { white-space: pre-wrap; word-break: break-all; font-size: 11.5px; }
  pre.out { font-size: 11.5px; }
  .rv-textrow { flex-wrap: nowrap; }

  .toast {
    left: 12px; right: 12px; bottom: calc(12px + var(--safe-bottom));
    max-width: none;
  }

  /* Fernsteuerung: Bildschirm bleibt sichtbar, Bedienleiste darunter */
  .rv-stage { min-height: 42vh; }
  .rv-stage canvas { max-height: 58vh; }
  .rv-keys .btn { min-width: var(--tap); min-height: var(--tap); font-size: 13px; padding: 8px; }
  .rv-bar { gap: 8px; }
  .rv-foot { font-size: 12px; }
}

/* Tabellen als Karten - sobald es wirklich eng wird */
@media (max-width: 700px) {
  table.responsive { display: block; }
  table.responsive thead { display: none; }
  table.responsive tbody { display: block; }
  table.responsive tr {
    display: block; background: var(--bg-2); border: 1px solid var(--line);
    border-radius: 10px; margin-bottom: 10px; padding: 8px 12px;
  }
  table.responsive tr:hover { background: var(--bg-2); }
  table.responsive td {
    display: flex; justify-content: space-between; align-items: center;
    gap: 12px; border: 0; padding: 5px 0; text-align: right;
  }
  table.responsive td::before {
    content: attr(data-label); color: var(--muted); font-size: 11px;
    text-transform: uppercase; letter-spacing: .4px; text-align: left;
    flex: 0 0 auto; padding-right: 8px;
  }
  table.responsive td:not([data-label]) { justify-content: flex-end; }
  table.responsive td[data-label="Hostname"] { font-size: 15px; }
  table.responsive td[data-label="Hostname"]::before { display: none; }
  table.responsive td[data-label="Gerät"] { font-size: 15px; }
  table.responsive td[data-label="Bezeichnung"] { font-size: 15px; }
  table.responsive td[data-label="Bezeichnung"]::before { display: none; }
  /* Zellen mit Schaltflaechen bekommen etwas Luft */
  table.responsive td.row { padding-top: 10px; margin-top: 4px; border-top: 1px dashed var(--line); }
  table.responsive td.row .btn { flex: 1 1 auto; justify-content: center; }
}

/* Sehr schmale Geraete */
@media (max-width: 380px) {
  .grid.cols-4 { grid-template-columns: 1fr 1fr; }
  .btn { font-size: 13px; padding: 9px 10px; }
  .rv-keys .btn { min-width: 40px; }
}

/* Querformat am Handy: Bedienelemente kompakter */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 480px) {
  .rv-stage { min-height: 60vh; }
  .rv-stage canvas { max-height: 70vh; }
  .topbar { position: static; }
}
