:root {
  --ink: #12212b;
  --muted: #647484;
  --line: #d8e2e8;
  --canvas: #eef4f7;
  --surface: #ffffff;
  --sidebar: #102a30;
  --sidebar-soft: #1a3a41;
  --teal: #087f8c;
  --teal-dark: #066672;
  --cyan: #0b9fba;
  --green: #18844b;
  --green-soft: #e6f5ec;
  --orange: #c76b09;
  --orange-soft: #fff1df;
  --red: #c83b3b;
  --red-soft: #fdebea;
  --blue: #2577b9;
  --blue-soft: #e7f2fb;
  --shadow: 0 2px 8px rgba(20, 44, 55, 0.07);
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  color: var(--ink);
  background: var(--canvas);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
  font-size: 14px;
  letter-spacing: 0;
}

button, input, select { font: inherit; letter-spacing: 0; }
button, select { cursor: pointer; }
button:disabled { cursor: wait; opacity: 0.58; }
a { color: inherit; }
[hidden] { display: none !important; }

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(120deg, rgba(8, 127, 140, 0.12), transparent 42%),
    #edf4f5;
}

.login-panel {
  width: min(420px, 100%);
  padding: 36px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 18px 48px rgba(22, 58, 65, 0.13);
}

.brand-mark {
  width: 58px;
  height: 58px;
  display: grid;
  place-items: center;
  color: white;
  background: var(--teal);
  border: 3px solid #55d6b0;
  border-radius: 50%;
  font-weight: 800;
  font-size: 19px;
}

.brand-mark.small { width: 42px; height: 42px; border-width: 2px; font-size: 14px; flex: 0 0 auto; }
.eyebrow { margin: 22px 0 6px; color: var(--teal); font-size: 12px; font-weight: 800; }
.login-panel h1 { margin: 0 0 28px; font-size: 28px; line-height: 1.2; }
.login-form { display: grid; gap: 10px; }
.login-form label { margin-top: 5px; font-weight: 700; }

input, select {
  width: 100%;
  min-height: 42px;
  padding: 9px 12px;
  color: var(--ink);
  background: white;
  border: 1px solid #cbd8df;
  border-radius: 6px;
  outline: none;
}

input:focus, select:focus { border-color: var(--teal); box-shadow: 0 0 0 3px rgba(8, 127, 140, 0.12); }
.form-error { min-height: 19px; margin: 2px 0 0; color: var(--red); font-weight: 600; }

.primary-button, .secondary-button, .icon-button {
  min-height: 40px;
  padding: 9px 16px;
  border-radius: 6px;
  border: 1px solid transparent;
  font-weight: 750;
}

.primary-button { color: white; background: var(--teal); border-color: var(--teal); }
.primary-button:hover { background: var(--teal-dark); }
.secondary-button { color: #324650; background: white; border-color: #cbd8df; }
.secondary-button:hover { background: #f5f8fa; border-color: #aebfc8; }
.full-width { width: 100%; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 238px minmax(0, 1fr); }

.sidebar {
  position: sticky;
  top: 0;
  z-index: 30;
  height: 100vh;
  display: flex;
  flex-direction: column;
  color: #e9f2f3;
  background: var(--sidebar);
  border-right: 1px solid #234149;
}

.sidebar-brand { min-height: 76px; display: flex; align-items: center; gap: 12px; padding: 16px 18px; border-bottom: 1px solid #28454b; }
.sidebar-brand strong { display: block; font-size: 16px; }
.sidebar-brand span { display: block; margin-top: 3px; color: #9fb4ba; font-size: 12px; }
.main-nav { display: grid; gap: 5px; padding: 18px 12px; }
.nav-button { min-height: 44px; display: flex; align-items: center; gap: 11px; padding: 8px 12px; color: #c9d9dc; background: transparent; border: 0; border-radius: 6px; text-align: left; font-weight: 700; }
.nav-button:hover { color: white; background: var(--sidebar-soft); }
.nav-button.active { color: white; background: var(--teal); }
.nav-icon { width: 24px; height: 24px; display: grid; place-items: center; border: 1px solid currentColor; border-radius: 50%; font-size: 11px; font-weight: 850; }
.sidebar-footer { margin-top: auto; padding: 14px; border-top: 1px solid #28454b; }
.signed-in-user { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.signed-in-user strong, .signed-in-user span { display: block; }
.signed-in-user span { margin-top: 2px; color: #9fb4ba; font-size: 12px; }
.user-avatar { width: 36px; height: 36px; display: grid; place-items: center; flex: 0 0 auto; color: white; background: var(--green); border-radius: 50%; font-weight: 800; }
.sidebar-footer .secondary-button { color: #e9f2f3; background: #1b353b; border-color: #36535a; }

.workspace { min-width: 0; }
.topbar { min-height: 76px; display: flex; align-items: center; gap: 16px; padding: 12px 28px; background: var(--surface); border-bottom: 1px solid var(--line); }
.page-title-wrap h1 { margin: 0; font-size: 23px; line-height: 1.2; }
.page-title-wrap p { margin: 4px 0 0; color: var(--muted); }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.last-updated { color: var(--muted); font-size: 12px; white-space: nowrap; }
.menu-button { display: none; padding-inline: 10px; color: var(--teal-dark); background: white; border-color: var(--line); }

.town-bar { min-height: 58px; display: flex; align-items: center; gap: 14px; padding: 8px 28px; background: #f8fbfc; border-bottom: 1px solid var(--line); }
.town-selector { min-width: 0; display: flex; gap: 5px; overflow-x: auto; scrollbar-width: thin; }
.town-button { min-width: 80px; min-height: 36px; padding: 6px 14px; color: #445965; background: white; border: 1px solid #cbd8df; border-radius: 6px; font-weight: 750; white-space: nowrap; }
.town-button.active { color: white; background: var(--teal); border-color: var(--teal); }
.read-only-badge { margin-left: auto; flex: 0 0 auto; padding: 5px 9px; color: #536975; background: #e8eef1; border-radius: 4px; font-size: 11px; font-weight: 800; text-transform: uppercase; }

.content { padding: 22px 28px 46px; }
.page { display: none; }
.page.active { display: block; }

.metric-grid { display: grid; grid-template-columns: repeat(6, minmax(145px, 1fr)); gap: 12px; margin-bottom: 18px; }
.metric { min-height: 112px; padding: 17px; background: var(--surface); border: 1px solid var(--line); border-top: 4px solid var(--teal); border-radius: 7px; box-shadow: var(--shadow); }
.metric.green { border-top-color: var(--green); }
.metric.orange { border-top-color: var(--orange); }
.metric.red { border-top-color: var(--red); }
.metric.blue { border-top-color: var(--blue); }
.metric-label { display: block; color: var(--muted); font-size: 12px; font-weight: 750; }
.metric-value { display: block; margin-top: 10px; font-size: 25px; line-height: 1; font-weight: 850; white-space: nowrap; }
.metric-detail { display: block; margin-top: 8px; color: var(--muted); font-size: 11px; }

.surface { margin-bottom: 18px; background: var(--surface); border: 1px solid var(--line); border-radius: 7px; box-shadow: var(--shadow); }
.section-heading { min-height: 68px; display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.section-heading h2 { margin: 0; font-size: 17px; }
.section-heading p { margin: 4px 0 0; color: var(--muted); font-size: 12px; }
.count-label { min-width: 28px; padding: 5px 8px; color: white; background: var(--red); border-radius: 50px; text-align: center; font-weight: 800; }

.health-summary { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.health-summary .status { padding-inline: 7px; }
.town-health-list { display: grid; grid-template-columns: repeat(4, minmax(190px, 1fr)); }
.town-health-item { min-width: 0; padding: 16px 18px; border-right: 1px solid var(--line); }
.town-health-item:last-child { border-right: 0; }
.health-item-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.health-item-town { display: flex; align-items: center; gap: 8px; min-width: 0; }
.health-item-town strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.health-score-row { display: flex; align-items: baseline; gap: 5px; margin-top: 14px; }
.health-score-value { font-size: 29px; line-height: 1; font-weight: 850; }
.health-score-max { color: var(--muted); font-size: 12px; }
.health-meter { height: 7px; margin-top: 10px; overflow: hidden; background: #e4ebee; border-radius: 4px; }
.health-meter span { display: block; height: 100%; background: var(--green); border-radius: inherit; }
.town-health-item.warning .health-meter span { background: var(--orange); }
.town-health-item.critical .health-meter span { background: var(--red); }
.health-risks { min-height: 34px; margin-top: 9px; color: var(--muted); font-size: 11px; line-height: 1.45; overflow-wrap: anywhere; }

.action-filters { display: flex; gap: 6px; padding: 10px 14px; overflow-x: auto; border-bottom: 1px solid var(--line); scrollbar-width: thin; }
.action-filter { min-height: 34px; padding: 6px 11px; color: #4a5e69; background: #f7f9fa; border: 1px solid #d3dee4; border-radius: 5px; font-size: 12px; font-weight: 750; white-space: nowrap; }
.action-filter.active { color: white; background: var(--teal); border-color: var(--teal); }
.action-filter-count { display: inline-grid; min-width: 19px; margin-left: 5px; padding: 1px 4px; place-items: center; color: inherit; background: rgba(255,255,255,0.18); border-radius: 8px; font-size: 10px; }
.action-filter:not(.active) .action-filter-count { background: #e5ecef; }

.table-wrap { width: 100%; overflow: auto; }
table { width: 100%; min-width: 940px; border-collapse: collapse; }
th, td { padding: 13px 14px; border-bottom: 1px solid #e3eaee; text-align: left; vertical-align: middle; }
th { color: #60717e; background: #f7f9fa; font-size: 11px; font-weight: 850; text-transform: uppercase; white-space: nowrap; }
td { color: #263943; }
tbody tr:hover { background: #f7fbfc; }
tbody tr:last-child td { border-bottom: 0; }
.empty-row td { height: 110px; color: var(--muted); text-align: center; }
.number-cell { font-variant-numeric: tabular-nums; font-weight: 750; white-space: nowrap; }
.muted-cell { color: var(--muted); }
.town-name { display: flex; align-items: center; gap: 9px; min-width: 115px; }
.town-badge { min-width: 38px; display: inline-grid; place-items: center; padding: 5px 7px; color: #075762; background: #dff3f4; border-radius: 4px; font-size: 11px; font-weight: 850; }
.town-name strong, .town-name span { display: block; }
.town-name span { margin-top: 2px; color: var(--muted); font-size: 11px; }
.status { display: inline-flex; align-items: center; gap: 6px; padding: 5px 8px; border-radius: 4px; font-size: 11px; font-weight: 800; white-space: nowrap; }
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status.good { color: var(--green); background: var(--green-soft); }
.status.warn { color: var(--orange); background: var(--orange-soft); }
.status.bad { color: var(--red); background: var(--red-soft); }
.status.info { color: var(--blue); background: var(--blue-soft); }
.status.neutral { color: #566773; background: #edf1f3; }
.open-link { color: var(--teal-dark); font-weight: 750; white-space: nowrap; text-decoration: none; }
.open-link:hover { text-decoration: underline; }

.attention-list { display: grid; }
.attention-item { min-height: 70px; display: grid; grid-template-columns: 92px minmax(150px, 1fr) minmax(180px, 2fr) auto; align-items: center; gap: 14px; padding: 12px 18px; border-bottom: 1px solid #e3eaee; }
.attention-item:last-child { border-bottom: 0; }
.attention-kind { font-size: 11px; font-weight: 850; text-transform: uppercase; }
.attention-kind.overdue { color: var(--red); }
.attention-kind.area { color: var(--orange); }
.attention-kind.uplink { color: var(--blue); }
.attention-kind.priority { color: #a44516; }
.attention-kind.stock { color: var(--orange); }
.attention-kind.recovery { color: #6d5aa7; }
.attention-kind.automation { color: var(--red); }
.attention-item.critical { box-shadow: inset 4px 0 var(--red); }
.attention-item.warning { box-shadow: inset 4px 0 var(--orange); }
.attention-item strong { overflow-wrap: anywhere; }
.attention-detail { color: var(--muted); overflow-wrap: anywhere; }
.empty-state { padding: 42px 20px; color: var(--muted); text-align: center; }

.automation-stats { display: grid; grid-template-columns: repeat(3, minmax(150px, 1fr)); border-bottom: 1px solid var(--line); }
.automation-stat { padding: 13px 18px; border-right: 1px solid var(--line); }
.automation-stat:last-child { border-right: 0; }
.automation-stat span, .automation-stat strong { display: block; }
.automation-stat span { color: var(--muted); font-size: 11px; font-weight: 750; text-transform: uppercase; }
.automation-stat strong { margin-top: 5px; font-size: 18px; }
.automation-table { min-width: 1000px; }
.shared-automation-list { border-top: 1px solid var(--line); }
.shared-automation-title { padding: 11px 18px 7px; color: var(--muted); font-size: 11px; font-weight: 850; text-transform: uppercase; }
.shared-automation-item { display: grid; grid-template-columns: minmax(220px, 1fr) 110px 150px auto; align-items: center; gap: 12px; padding: 10px 18px; border-top: 1px solid #edf1f3; }
.shared-automation-item strong { overflow-wrap: anywhere; }

.filter-bar { display: grid; grid-template-columns: minmax(240px, 2fr) repeat(3, minmax(150px, 1fr)) auto auto; gap: 9px; margin-bottom: 14px; padding: 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 7px; box-shadow: var(--shadow); }
#ticket-filters { grid-template-columns: minmax(260px, 2fr) minmax(150px, 1fr) minmax(150px, 1fr) auto auto; }
#inventory-filters { grid-template-columns: minmax(260px, 2fr) minmax(170px, 1fr) auto auto; }
#technician-filters { grid-template-columns: minmax(260px, 1fr) auto auto; }
.table-surface { min-height: 300px; }

.search-hero { padding: 20px; background: var(--surface); border: 1px solid var(--line); border-radius: 7px; box-shadow: var(--shadow); }
.search-hero label { display: block; margin-bottom: 9px; font-size: 16px; font-weight: 800; }
.search-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 9px; }
.result-summary { min-height: 38px; display: flex; align-items: center; color: var(--muted); font-weight: 650; }
.search-results { display: grid; gap: 14px; }
.search-group { background: var(--surface); border: 1px solid var(--line); border-radius: 7px; box-shadow: var(--shadow); }
.search-group .section-heading { min-height: 56px; }
.search-group table { min-width: 760px; }

.toast { position: fixed; z-index: 100; right: 20px; bottom: 20px; max-width: min(380px, calc(100vw - 40px)); padding: 11px 15px; color: white; background: #263943; border-radius: 6px; box-shadow: 0 8px 24px rgba(0,0,0,0.22); opacity: 0; pointer-events: none; transform: translateY(8px); transition: 160ms ease; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--red); }
.loading { position: relative; opacity: 0.62; pointer-events: none; }

@media (max-width: 1280px) {
  .metric-grid { grid-template-columns: repeat(3, minmax(160px, 1fr)); }
  .town-health-list { grid-template-columns: repeat(2, minmax(190px, 1fr)); }
  .town-health-item:nth-child(2) { border-right: 0; }
  .town-health-item:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
  .filter-bar { grid-template-columns: repeat(2, minmax(180px, 1fr)); }
  .filter-bar .primary-button, .filter-bar .secondary-button { width: 100%; }
}

@media (max-width: 860px) {
  .app-shell { display: block; }
  .sidebar { position: fixed; left: 0; width: min(286px, 84vw); transform: translateX(-102%); transition: transform 180ms ease; box-shadow: 10px 0 28px rgba(0,0,0,0.24); }
  .sidebar.open { transform: translateX(0); }
  .menu-button { display: inline-flex; align-items: center; }
  .topbar { padding-inline: 16px; }
  .town-bar { padding-inline: 16px; }
  .content { padding: 16px 16px 36px; }
  .last-updated { display: none; }
  .metric-grid { grid-template-columns: repeat(2, minmax(140px, 1fr)); }
  .attention-item { grid-template-columns: 76px minmax(0, 1fr) auto; }
  .attention-detail { grid-column: 2 / -1; }
  .shared-automation-item { grid-template-columns: minmax(180px, 1fr) auto; }
  .shared-automation-item > :nth-child(3), .shared-automation-item > :nth-child(4) { grid-column: 2; }
  .filter-bar, #ticket-filters, #inventory-filters, #technician-filters { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 560px) {
  .login-view { padding: 14px; }
  .login-panel { padding: 26px 20px; }
  .topbar { min-height: 66px; gap: 10px; }
  .menu-button, .topbar #refresh-button { width: 42px; min-width: 42px; padding: 0; font-size: 0; }
  .menu-button::before { content: "\2630"; font-size: 20px; line-height: 1; }
  .topbar #refresh-button::before { content: "\21bb"; font-size: 24px; line-height: 1; }
  .page-title-wrap h1 { font-size: 19px; }
  .page-title-wrap p { display: none; }
  .topbar-actions { gap: 6px; }
  .topbar .secondary-button { padding-inline: 10px; }
  .town-bar { padding-block: 7px; }
  .read-only-badge { display: none; }
  .metric-grid { gap: 8px; }
  .metric { min-height: 98px; padding: 13px; }
  .metric-value { font-size: 21px; }
  .town-health-list { grid-template-columns: 1fr; }
  .town-health-item { border-right: 0; border-bottom: 1px solid var(--line); }
  .town-health-item:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
  .town-health-item:last-child { border-bottom: 0; }
  .health-summary { display: none; }
  .section-heading { min-height: 62px; padding: 12px 14px; }
  .attention-item { grid-template-columns: 68px minmax(0, 1fr); gap: 8px; padding: 12px 14px; }
  .attention-item .open-link { grid-column: 2; }
  .attention-detail { grid-column: 2; }
  .automation-stats { grid-template-columns: 1fr 1fr; }
  .automation-stat { border-bottom: 1px solid var(--line); }
  .automation-stat:last-child { grid-column: 1 / -1; border-bottom: 0; }
  .shared-automation-item { grid-template-columns: 1fr auto; padding-inline: 14px; }
  .filter-bar, #ticket-filters, #inventory-filters, #technician-filters { grid-template-columns: 1fr; }
  .search-row { grid-template-columns: 1fr; }
  .search-hero { padding: 14px; }
}
