:root {
  color-scheme: light;
  --ink: #18211e;
  --muted: #66716d;
  --line: #dbe1de;
  --panel: #ffffff;
  --canvas: #f4f6f4;
  --brand: #157a5a;
  --brand-dark: #0d5a41;
  --danger: #b42318;
  --warning: #a15c00;
  --shadow: 0 16px 40px rgba(27, 46, 39, .08);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; min-width: 320px; color: var(--ink); background: var(--canvas); }
button, input, select { font: inherit; }
button { border: 0; border-radius: 9px; padding: .72rem 1rem; color: white; background: var(--brand); cursor: pointer; font-weight: 650; }
button:hover { background: var(--brand-dark); }
button.secondary { color: var(--ink); background: #edf1ef; }
button.secondary:hover { background: #e1e7e4; }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 2rem; padding: 2rem max(4vw, 1.25rem); color: white; background: #142b24; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(1.55rem, 3vw, 2.4rem); letter-spacing: -.035em; }
h2 { margin-bottom: 0; font-size: 1.2rem; }
.eyebrow { margin-bottom: .4rem; color: #79c9a9; font-size: .72rem; font-weight: 800; letter-spacing: .15em; }
.connection-box { display: flex; align-items: center; gap: .7rem; white-space: nowrap; }
.status-dot { width: .7rem; height: .7rem; border-radius: 50%; background: #8e9b96; box-shadow: 0 0 0 4px rgba(255,255,255,.08); }
.status-dot.online { background: #61d39f; }
.status-dot.connecting { background: #f5b942; }

main { display: grid; gap: 1.25rem; max-width: 1500px; margin: 0 auto; padding: 1.5rem max(4vw, 1.25rem) 4rem; }
.panel { border: 1px solid var(--line); border-radius: 14px; padding: 1.25rem; background: var(--panel); box-shadow: var(--shadow); }
.settings-panel { display: grid; grid-template-columns: auto minmax(220px, 1fr) auto; align-items: center; gap: .8rem; }
.settings-panel p { margin: 0; color: var(--muted); font-size: .84rem; }
.dashboard-grid { display: grid; grid-template-columns: minmax(280px, .8fr) minmax(520px, 1.5fr); gap: 1.25rem; align-items: start; }
.section-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.15rem; }

label { display: grid; gap: .42rem; color: #39443f; font-size: .84rem; font-weight: 650; }
input, select { width: 100%; border: 1px solid #cbd4d0; border-radius: 8px; padding: .7rem .75rem; color: var(--ink); background: white; outline: none; }
input:focus, select:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(21,122,90,.12); }
.field-grid { display: grid; gap: .9rem; }
.two-columns { grid-template-columns: repeat(2, minmax(0, 1fr)); }

.device-list { display: grid; gap: .65rem; }
.device-card { display: grid; grid-template-columns: 1fr auto; gap: .35rem .8rem; border: 1px solid var(--line); border-radius: 10px; padding: .85rem; cursor: pointer; }
.device-card:hover { border-color: #8dbca9; }
.device-card strong { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.device-card span { color: var(--muted); font-size: .78rem; }
.role-badge { align-self: start; border-radius: 999px; padding: .2rem .5rem; color: #126246 !important; background: #def4ea; font-weight: 750; }
.empty-state { color: var(--muted); padding: 2rem 0; text-align: center; }

.tabs { display: flex; gap: .45rem; margin: 1.15rem 0; border-bottom: 1px solid var(--line); }
.tab { border-radius: 7px 7px 0 0; color: var(--muted); background: transparent; }
.tab:hover { color: var(--ink); background: #f2f5f3; }
.tab.active { color: var(--brand); background: #e7f4ee; }
.command-form { display: none; }
.command-form.active { display: grid; gap: 1rem; }
.command-form p { margin-bottom: 0; color: var(--muted); font-size: .84rem; }
.command-form > button { justify-self: start; }
.joystick-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .65rem; }
fieldset { display: grid; gap: .6rem; margin: 0; border: 1px solid var(--line); border-radius: 10px; padding: .75rem; }
legend { padding: 0 .35rem; color: var(--brand-dark); font-weight: 800; }
.form-error { min-height: 1.3rem; margin: .8rem 0 0; color: var(--danger); font-size: .86rem; }

.event-table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .84rem; }
th, td { border-bottom: 1px solid var(--line); padding: .7rem .55rem; text-align: left; vertical-align: top; }
th { color: var(--muted); font-size: .72rem; letter-spacing: .04em; text-transform: uppercase; }
tbody tr:not(.empty-row) { cursor: pointer; }
tbody tr:not(.empty-row):hover { background: #f5f8f6; }
.event-failed { color: var(--danger); font-weight: 700; }
.event-completed { color: var(--brand); font-weight: 700; }
.event-detail { margin-top: 1rem; border-radius: 10px; padding: 1rem; background: #15211d; color: #dce9e4; }
.event-detail h3 { margin-bottom: .7rem; color: white; font-size: .9rem; }
pre { margin: 0; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; font: .78rem/1.55 ui-monospace, SFMono-Regular, Menlo, monospace; }

@media (max-width: 900px) {
  .dashboard-grid { grid-template-columns: 1fr; }
  .settings-panel { grid-template-columns: 1fr; }
}
@media (max-width: 620px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .two-columns, .joystick-grid { grid-template-columns: 1fr 1fr; }
}
