:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --panel: #ffffff;
  --panel-strong: #f8fafc;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e5e7eb;
  --blue: #238bef;
  --blue-dark: #176fd0;
  --blue-soft: #eaf4ff;
  --green: #22c55e;
  --green-dark: #15803d;
  --green-soft: #eaf8f0;
  --amber: #d97706;
  --amber-soft: #fff7e6;
  --red: #dc2626;
  --red-soft: #fff1f2;
  --shadow: 0 10px 26px rgba(15, 23, 42, 0.06);
  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; background: var(--bg); color: var(--ink); }
button, input, select { font: inherit; }

.app-shell { display: grid; grid-template-columns: 248px minmax(0, 1fr); min-height: 100vh; }
.sidebar { background: var(--panel); color: var(--ink); padding: 20px; border-right: 1px solid var(--line); box-shadow: 8px 0 24px rgba(15, 23, 42, 0.035); }
.brand { display: flex; align-items: center; gap: 12px; min-height: 48px; }
.brand-logo { display: block; width: min(180px, 100%); height: auto; border-radius: 10px; }
.compact-brand .brand-logo { width: min(220px, 100%); }
.brand-mark { display: grid; place-items: center; width: 46px; height: 46px; border-radius: 12px; background: var(--blue); color: #ffffff; font-weight: 950; font-size: 11px; line-height: 1; letter-spacing: 0; text-transform: uppercase; box-shadow: 0 12px 22px rgba(35, 139, 239, 0.22); }
.brand-mark span { display: block; color: inherit; margin: 0; }
.brand strong, .brand span { display: block; }
.brand strong { letter-spacing: 0; }
.brand span { color: var(--muted); font-size: 13px; margin-top: 2px; }
.compact-brand > div > span { color: var(--muted); }
.compact-brand .brand-mark span { color: inherit; }
.nav-list { display: grid; gap: 6px; margin-top: 34px; }
.nav-item { width: 100%; min-height: 42px; border: 0; border-radius: 10px; padding: 0 12px; background: transparent; color: var(--muted); text-align: left; cursor: pointer; font-weight: 700; }
.nav-item.active, .nav-item:hover { background: var(--blue); color: #ffffff; box-shadow: 0 10px 18px rgba(35, 139, 239, 0.18); }

.main { display: grid; grid-template-rows: auto auto auto minmax(0, 1fr); gap: 18px; min-width: 0; padding: 22px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.eyebrow { margin: 0 0 4px; color: var(--blue); font-size: 12px; font-weight: 850; letter-spacing: 0; text-transform: uppercase; }
h1, h2, p { margin: 0; }
h1 { font-size: 28px; line-height: 1.1; }
h2 { font-size: 17px; }
.top-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.primary-button, .ghost-button { min-height: 40px; border-radius: 10px; padding: 0 14px; border: 1px solid var(--line); cursor: pointer; }
.primary-button { background: var(--blue); border-color: var(--blue); color: #ffffff; font-weight: 850; box-shadow: 0 10px 18px rgba(35, 139, 239, 0.16); }
.primary-button:hover { background: var(--blue-dark); border-color: var(--blue-dark); color: #ffffff; }
.ghost-button { background: white; color: var(--ink); }
.ghost-button:hover { border-color: var(--blue); color: var(--blue-dark); background: var(--blue-soft); }
.ghost-button.compact { min-height: 38px; }
.danger-button { border-color: #f0c5c5; color: var(--red); }
.danger-button:hover { border-color: var(--red); color: var(--red); background: #fff5f5; }

.metrics { display: grid; grid-template-columns: repeat(6, minmax(130px, 1fr)); gap: 12px; }
.metric { min-height: 86px; border: 1px solid var(--line); border-radius: 14px; background: var(--panel); padding: 14px; box-shadow: var(--shadow); }
.metric:first-child, .metric:nth-child(2), .metric:nth-child(4) { border-top: 3px solid var(--blue); }
.metric.warning { border-left-color: var(--amber); }
.metric.danger { border-left-color: var(--red); }
.metric span { display: block; color: var(--muted); font-size: 13px; }
.metric strong { display: block; margin-top: 8px; font-size: 28px; line-height: 1; }

.form-panel { border: 1px solid var(--line); border-radius: 14px; background: var(--panel); padding: 16px; box-shadow: var(--shadow); }
.hidden { display: none; }
.device-form, .wide-form { display: grid; grid-template-columns: repeat(7, minmax(120px, 1fr)); gap: 10px; margin-top: 12px; }
.device-form input, .wide-form input, .wide-form select, .login-form input { min-height: 40px; border: 1px solid var(--line); border-radius: 10px; padding: 0 10px; background: white; color: var(--ink); }
.device-form input:focus, .wide-form input:focus, .wide-form select:focus, .login-form input:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(35, 139, 239, 0.12); outline: none; }
.token-output { margin-top: 10px; color: var(--blue-dark); font-size: 13px; overflow-wrap: anywhere; }

.finance-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.data-panel { min-width: 0; border: 1px solid var(--line); border-radius: 14px; background: var(--panel); box-shadow: var(--shadow); overflow: visible; }
.data-list { display: grid; gap: 8px; max-height: none; overflow: visible; padding: 12px; }
.data-item { border: 1px solid var(--line); border-radius: 12px; background: white; padding: 12px; }
.payment-row { display: flex; align-items: center; justify-content: flex-start; flex-wrap: wrap; gap: 8px; margin-top: 10px; color: var(--ink); font-weight: 800; }
.payment-row > span:first-child { margin-right: auto; }
.payment-row .ghost-button,
.payment-row .primary-button,
.payment-row select {
  flex: 0 1 auto;
}
.device .payment-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(128px, max-content));
  justify-content: start;
}
.device .payment-row > span:first-child {
  display: none;
}
.device .payment-row .ghost-button {
  width: 100%;
  min-height: 36px;
  padding: 0 10px;
  white-space: normal;
}
.qr-box { display: grid; place-items: center; margin-top: 12px; padding: 10px; border: 1px dashed #bfdbfe; border-radius: 12px; background: var(--blue-soft); overflow-wrap: anywhere; }
.qr-box canvas { width: 260px !important; height: 260px !important; cursor: pointer; }
.modal-open { overflow: hidden; }
.qr-modal { position: fixed; inset: 0; z-index: 10000; display: grid; place-items: center; padding: 18px; }
.qr-modal.hidden { display: none; }
.qr-modal-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, 0.52); backdrop-filter: blur(4px); }
.qr-modal-panel { position: relative; width: min(520px, 100%); border: 1px solid var(--line); border-radius: 18px; background: var(--panel); padding: 24px; box-shadow: 0 32px 80px rgba(15, 23, 42, 0.28); text-align: center; }
.qr-modal-close { position: absolute; top: 12px; right: 12px; display: grid; place-items: center; width: 34px; height: 34px; border: 1px solid var(--line); border-radius: 999px; background: #ffffff; color: var(--ink); cursor: pointer; font-size: 22px; line-height: 1; }
.qr-modal-close:hover { border-color: var(--blue); color: var(--blue-dark); background: var(--blue-soft); }
.qr-modal-canvas { display: grid; place-items: center; margin: 18px auto 12px; padding: 14px; border: 1px solid var(--line); border-radius: 16px; background: #ffffff; }
.qr-modal-canvas canvas { width: min(76vw, 420px) !important; height: min(76vw, 420px) !important; }
.qr-modal-token { max-width: 100%; color: var(--muted); font-size: 13px; line-height: 1.35; overflow-wrap: anywhere; }

.workspace { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 16px; min-height: 520px; }
.map-panel, .asset-list { min-width: 0; border: 1px solid var(--line); border-radius: 14px; background: var(--panel); box-shadow: var(--shadow); }
.map-panel { display: grid; grid-template-rows: auto minmax(360px, 1fr); overflow: hidden; }
.map-toolbar { display: flex; align-items: center; gap: 10px; padding: 14px; border-bottom: 1px solid var(--line); background: var(--panel-strong); }
.toolbar-note { color: var(--blue-dark); font-size: 12px; font-weight: 750; padding: 4px 8px; border-radius: 999px; background: var(--blue-soft); }
.asset-map { width: 100%; min-height: 360px; }
.asset-list { display: grid; grid-template-rows: auto minmax(0, 1fr); overflow: visible; }
.list-header { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; padding: 16px; border-bottom: 1px solid var(--line); }
.list-header span { color: var(--muted); font-size: 13px; }
.device-list { display: grid; align-content: start; gap: 8px; overflow: visible; padding: 12px; }
.device { border: 1px solid var(--line); border-radius: 12px; background: white; padding: 12px; cursor: pointer; text-align: left; }
.device:hover, .device.active { border-color: #bfdbfe; background: var(--blue-soft); }
.device-row { display: flex; justify-content: space-between; gap: 8px; align-items: start; }
.device-title { font-weight: 800; }
.device-meta { margin-top: 6px; color: var(--muted); font-size: 12px; line-height: 1.3; }
.status-pill { flex: 0 0 auto; border-radius: 999px; padding: 3px 7px; font-size: 11px; font-weight: 850; background: var(--green-soft); color: var(--green-dark); }
.status-pill.success { background: var(--green-soft); color: var(--green-dark); }
.status-pill.warning { background: var(--amber-soft); color: var(--amber); }
.status-pill.danger { background: var(--red-soft); color: var(--red); }
.status-pill.offline { background: var(--amber-soft); color: var(--amber); }
.note-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 9px; }
.note-chip { display: inline-flex; align-items: center; min-height: 22px; border-radius: 999px; padding: 0 8px; font-size: 11px; font-weight: 850; line-height: 1; white-space: nowrap; }
.note-chip.info { background: var(--blue-soft); color: var(--blue-dark); }
.note-chip.success { background: var(--green-soft); color: var(--green-dark); }
.note-chip.warning { background: var(--amber-soft); color: var(--amber); }
.note-chip.danger { background: var(--red-soft); color: var(--red); }
.note-chip.money { background: #eef2ff; color: #3730a3; }
.note-chip.neutral { background: var(--panel-strong); color: var(--muted); border: 1px solid var(--line); }
.command-history { display: grid; gap: 6px; margin-top: 10px; padding: 8px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel-strong); }
.command-title { color: var(--muted); font-size: 11px; font-weight: 900; text-transform: uppercase; }
.command-line { display: grid; grid-template-columns: minmax(90px, 1fr) auto; align-items: center; gap: 6px; color: var(--ink); font-size: 12px; }
.command-line small { grid-column: 1 / -1; color: var(--muted); overflow-wrap: anywhere; }
.command-empty { color: var(--muted); font-size: 12px; }
.guide-grid { display: grid; grid-template-columns: repeat(4, minmax(150px, 1fr)); gap: 10px; margin-top: 12px; }
.guide-grid div { border: 1px solid var(--line); border-radius: 10px; background: var(--panel-strong); padding: 10px; }
.guide-grid strong { display: block; font-size: 13px; }
.guide-grid span { display: block; margin-top: 6px; color: var(--muted); font-size: 12px; line-height: 1.35; }

body[data-view="map"] .finance-grid,
body[data-view="devices"] .finance-grid,
body[data-view="contracts"] .workspace,
body[data-view="finance"] .workspace,
body[data-view="incidents"] .workspace,
body[data-view="apps"] .workspace {
  display: none;
}

body[data-view="contracts"] [data-panel-view],
body[data-view="finance"] [data-panel-view],
body[data-view="incidents"] [data-panel-view],
body[data-view="apps"] [data-panel-view] {
  display: none;
}

body[data-view="contracts"] [data-panel-view="contracts"],
body[data-view="finance"] [data-panel-view="finance"],
body[data-view="incidents"] [data-panel-view="incidents"],
body[data-view="apps"] [data-panel-view="apps"] {
  display: block;
}

body[data-view="contracts"] .finance-grid,
body[data-view="finance"] .finance-grid,
body[data-view="incidents"] .finance-grid,
body[data-view="apps"] .finance-grid {
  grid-template-columns: 1fr;
}

body[data-view="devices"] .workspace {
  grid-template-columns: 1fr;
}

body[data-view="devices"] .map-panel {
  display: none;
}

.client-portal { background: var(--bg); }
.client-shell { display: grid; gap: 18px; min-height: 100vh; padding: 22px; }
.client-header { display: flex; align-items: center; justify-content: space-between; gap: 14px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); padding: 16px; box-shadow: var(--shadow); }
.client-header .brand strong { color: var(--ink); }
.client-metrics { grid-template-columns: repeat(4, minmax(130px, 1fr)); }
.client-workspace { min-height: 620px; }
.client-panels { grid-template-columns: 1fr 1fr; }

.login-body { display: grid; place-items: center; min-height: 100vh; padding: 18px; background: var(--bg); }
.login-panel { width: min(460px, 100%); border: 1px solid var(--line); border-radius: 16px; background: white; padding: 24px; box-shadow: 0 24px 60px rgba(15, 23, 42, 0.08); }
.login-copy { margin-top: 24px; padding: 18px; border-radius: 14px; background: var(--panel-strong); border: 1px solid var(--line); }
.login-copy h1 { font-size: 24px; }
.login-copy p:last-child { margin-top: 8px; color: var(--muted); line-height: 1.45; }
.login-form { display: grid; gap: 10px; margin-top: 24px; }
.login-form label { color: var(--muted); font-size: 13px; }
.form-error { color: var(--red); font-size: 13px; }

@media (max-width: 1040px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { padding: 14px; }
  .nav-list { grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 6px; margin-top: 14px; }
  .nav-item { min-height: 38px; padding: 0 8px; text-align: center; font-size: 13px; }
  .workspace { grid-template-columns: 1fr; }
  .finance-grid { grid-template-columns: 1fr; }
  .guide-grid { grid-template-columns: 1fr 1fr; }
  .device-form, .wide-form { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 680px) {
  .main { padding: 14px; }
  .client-shell { padding: 14px; }
  .client-header { align-items: stretch; flex-direction: column; }
  .topbar, .map-toolbar { align-items: stretch; flex-direction: column; }
  .top-actions, .top-actions form, .top-actions button, .map-toolbar button { width: 100%; }
  .nav-list { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .client-metrics { grid-template-columns: 1fr; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .guide-grid { grid-template-columns: 1fr; }
  .device-form, .wide-form { grid-template-columns: 1fr; }
  h1 { font-size: 24px; }
}
