:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --text: #1c2733;
  --text-muted: #6b7684;
  --border: #e1e6ec;
  --input-bg: #ffffff;

  --header-bg: #1f4e78;
  --header-text: #ffffff;
  --header-text-muted: #d9e6f2;

  --navy: #1f4e78;
  --navy-dark: #163a58;
  --accent: #2e86de;

  --green: #2e7d32;
  --green-bg: #e8f5e9;
  --red: #c62828;
  --red-bg: #fdecea;
  --amber: #b7791f;
  --amber-bg: #fff8e1;

  --progress-track: #e6ebf1;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #10161d;
    --card: #1a222c;
    --text: #e7edf3;
    --text-muted: #97a3b0;
    --border: #2b3a49;
    --input-bg: #1e2733;

    --header-bg: #12233a;
    --header-text: #eaf1f8;
    --header-text-muted: #a9c0d6;

    --navy: #3d7cb8;
    --navy-dark: #2e6da8;
    --accent: #5aa9e6;

    --green: #66bb6a;
    --green-bg: #1b3a1e;
    --red: #ef5350;
    --red-bg: #3a1c1c;
    --amber: #ffb74d;
    --amber-bg: #3a2e12;

    --progress-track: #26313d;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #10161d;
  --card: #1a222c;
  --text: #e7edf3;
  --text-muted: #97a3b0;
  --border: #2b3a49;
  --input-bg: #1e2733;

  --header-bg: #12233a;
  --header-text: #eaf1f8;
  --header-text-muted: #a9c0d6;

  --navy: #3d7cb8;
  --navy-dark: #2e6da8;
  --accent: #5aa9e6;

  --green: #66bb6a;
  --green-bg: #1b3a1e;
  --red: #ef5350;
  --red-bg: #3a1c1c;
  --amber: #ffb74d;
  --amber-bg: #3a2e12;

  --progress-track: #26313d;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html, body {
  overflow-x: hidden;
  width: 100%;
}

body {
  margin: 0;
  font-family: -apple-system, 'Segoe UI', Roboto, Tahoma, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

header.topbar {
  background: var(--header-bg);
  color: var(--header-text);
  padding: 0.7rem 1rem;
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

header.topbar h1 {
  font-size: 1.05rem;
  margin: 0;
  font-weight: 600;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.topbar-actions {
  display: flex;
  gap: 0.4rem;
  flex-shrink: 0;
}

.icon-btn {
  width: 48px;
  height: 48px;
  min-width: 48px;
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 8px;
  color: var(--header-text);
  font-size: 1.15rem;
  cursor: pointer;
  padding: 0;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

nav.tabs {
  display: none;
  flex-direction: column;
  gap: 0.2rem;
  margin-top: 0.6rem;
}

nav.tabs.open {
  display: flex;
}

nav.tabs a {
  color: var(--header-text-muted);
  text-decoration: none;
  padding: 0.65rem 0.9rem;
  border-radius: 6px;
  font-size: 0.95rem;
  font-weight: 500;
  min-height: 48px;
  display: flex;
  align-items: center;
}

nav.tabs a:hover {
  background: rgba(255, 255, 255, 0.1);
}

nav.tabs a.active {
  background: #fff;
  color: var(--navy);
}

main {
  padding: 1rem;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.grid-main {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

.grid-thirds {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.1rem 1.1rem;
  box-shadow: 0 1px 3px rgba(20, 30, 50, 0.05);
  min-width: 0;
}

.card h2, .card h3 {
  margin-top: 0;
}

.tile {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.tile .tile-label {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  font-weight: 600;
}

.tile .tile-figures {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.tile .figure {
  display: flex;
  flex-direction: column;
}

.tile .figure .value {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--navy);
}

.tile .figure .unit {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.progress-bar {
  height: 10px;
  background: var(--progress-track);
  border-radius: 5px;
  overflow: hidden;
  margin-top: 0.25rem;
}

.progress-bar > span {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 5px;
}

.progress-bar.over > span {
  background: var(--green);
}

.progress-bar.under > span {
  background: var(--amber);
}

.pct-badge {
  font-size: 0.78rem;
  font-weight: 700;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  white-space: nowrap;
}

.pct-badge.good {
  color: var(--green);
  background: var(--green-bg);
}

.pct-badge.bad {
  color: var(--red);
  background: var(--red-bg);
}

.pct-badge.mid {
  color: var(--amber);
  background: var(--amber-bg);
}

.table-scroll {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: 8px;
}

table {
  width: 100%;
  min-width: 560px;
  border-collapse: collapse;
  font-size: 0.92rem;
}

table th, table td {
  text-align: left;
  padding: 0.65rem 0.7rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

table th {
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

table tbody tr:hover {
  background: rgba(46, 134, 222, 0.05);
}

form.stacked-form {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.9rem;
  align-items: end;
}

form.stacked-form .field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  min-width: 0;
}

label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
}

input, select, textarea {
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 1rem;
  background: var(--input-bg);
  color: var(--text);
  min-height: 48px;
  width: 100%;
  font-family: inherit;
}

textarea {
  min-height: 96px;
}

input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button {
  background: var(--navy);
  color: #fff;
  border: none;
  padding: 0.6rem 1.1rem;
  border-radius: 6px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 48px;
  min-width: 48px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button:hover {
  background: var(--navy-dark);
}

button.secondary {
  background: transparent;
  color: var(--navy);
  border: 1px solid var(--navy);
}

button.secondary:hover {
  background: var(--green-bg);
  background: rgba(46, 134, 222, 0.08);
}

button.danger {
  background: transparent;
  color: var(--red);
  border: 1px solid var(--red);
  padding: 0.5rem 0.8rem;
  font-size: 0.85rem;
}

button.danger:hover {
  background: var(--red-bg);
}

.section-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.8rem;
}

.muted {
  color: var(--text-muted);
  font-size: 0.88rem;
}

.toast {
  position: fixed;
  bottom: 1.3rem;
  right: 1.3rem;
  left: 1.3rem;
  background: var(--navy-dark);
  color: #fff;
  padding: 0.8rem 1.1rem;
  border-radius: 8px;
  font-size: 0.9rem;
  box-shadow: 0 4px 14px rgba(0,0,0,0.3);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
  z-index: 100;
  text-align: center;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.toast.error {
  background: var(--red);
}

.roster-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.7rem;
}

.roster-role {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.7rem;
}

.roster-role .role-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 700;
}

.roster-role .role-name {
  font-size: 0.98rem;
  font-weight: 600;
  margin-top: 0.15rem;
  word-break: break-word;
}

.empty-state {
  color: var(--text-muted);
  font-style: italic;
  padding: 1rem 0;
  white-space: normal;
}

.report-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.updated-at {
  font-size: 0.78rem;
  color: var(--text-muted);
  text-align: right;
  margin-bottom: 0.6rem;
}

.scope-buttons {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.nav-user {
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
  padding: 0.5rem 0.9rem 0.8rem;
  margin-bottom: 0.3rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.nav-user-name {
  color: var(--header-text-muted);
  font-size: 0.85rem;
}

.nav-user-name strong {
  color: var(--header-text);
}

.nav-logout {
  align-self: flex-start;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.35);
  color: var(--header-text);
  min-height: 40px;
  padding: 0.3rem 0.8rem;
  font-size: 0.85rem;
}

.nav-logout:hover {
  background: rgba(255, 255, 255, 0.12);
}

.tunnel-banner {
  background: var(--green-bg);
  border: 1px solid var(--green);
  border-radius: 8px;
  padding: 0.8rem 1rem;
  margin-bottom: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  font-size: 0.9rem;
}

.tunnel-banner a {
  color: var(--green);
  font-weight: 600;
  word-break: break-all;
}

@media (min-width: 640px) {
  .tunnel-banner {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
}

/* Tablet and up */
@media (min-width: 640px) {
  .grid {
    grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  }

  form.stacked-form {
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  }

  main {
    padding: 1.5rem;
  }
}

/* Desktop: persistent nav instead of hamburger */
@media (min-width: 768px) {
  header.topbar {
    padding: 0.9rem 1.5rem;
  }

  .topbar-row {
    gap: 1rem;
  }

  header.topbar h1 {
    font-size: 1.15rem;
  }

  #navToggle {
    display: none;
  }

  nav.tabs {
    display: flex !important;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 0.3rem;
    margin-top: 0;
  }

  nav.tabs a {
    min-height: auto;
  }

  .nav-user {
    flex-direction: row;
    align-items: center;
    border-bottom: none;
    border-right: 1px solid rgba(255, 255, 255, 0.15);
    padding: 0 0.9rem 0 0;
    margin-bottom: 0;
    margin-right: 0.3rem;
  }

  .nav-logout {
    min-height: 36px;
  }

  .grid-main {
    grid-template-columns: 2fr 1fr;
  }

  .grid-thirds {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* Manual layout override (View toggle in header) — beats the automatic
   width-based rules above regardless of actual screen size. */
:root[data-layout="desktop"] header.topbar {
  padding: 0.9rem 1.5rem;
}

:root[data-layout="desktop"] .topbar-row {
  gap: 1rem;
}

:root[data-layout="desktop"] header.topbar h1 {
  font-size: 1.15rem;
}

:root[data-layout="desktop"] #navToggle {
  display: none !important;
}

:root[data-layout="desktop"] nav.tabs {
  display: flex !important;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-top: 0;
}

:root[data-layout="desktop"] nav.tabs a {
  min-height: auto;
}

:root[data-layout="desktop"] .nav-user {
  flex-direction: row;
  align-items: center;
  border-bottom: none;
  border-right: 1px solid rgba(255, 255, 255, 0.15);
  padding: 0 0.9rem 0 0;
  margin-bottom: 0;
  margin-right: 0.3rem;
}

:root[data-layout="desktop"] .nav-logout {
  min-height: 36px;
}

:root[data-layout="desktop"] .grid {
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
}

:root[data-layout="desktop"] .grid-main {
  grid-template-columns: 2fr 1fr;
}

:root[data-layout="desktop"] .grid-thirds {
  grid-template-columns: repeat(3, 1fr);
}

:root[data-layout="desktop"] form.stacked-form {
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
}

:root[data-layout="desktop"] main {
  padding: 1.5rem;
}

:root[data-layout="mobile"] header.topbar {
  padding: 0.7rem 1rem;
}

:root[data-layout="mobile"] header.topbar h1 {
  font-size: 1.05rem;
}

:root[data-layout="mobile"] #navToggle {
  display: flex !important;
}

:root[data-layout="mobile"] nav.tabs {
  display: none !important;
  flex-direction: column;
  margin-top: 0.6rem;
}

:root[data-layout="mobile"] nav.tabs.open {
  display: flex !important;
}

:root[data-layout="mobile"] nav.tabs a {
  min-height: 48px;
}

:root[data-layout="mobile"] .nav-user {
  flex-direction: column !important;
  align-items: stretch;
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
  border-right: none;
  padding: 0.5rem 0.9rem 0.8rem;
  margin-bottom: 0.3rem;
  margin-right: 0;
}

:root[data-layout="mobile"] .nav-logout {
  min-height: 44px;
}

:root[data-layout="mobile"] .grid,
:root[data-layout="mobile"] .grid-main,
:root[data-layout="mobile"] .grid-thirds {
  grid-template-columns: 1fr;
}

:root[data-layout="mobile"] form.stacked-form {
  grid-template-columns: 1fr;
}

:root[data-layout="mobile"] main {
  padding: 1rem;
}
