/* ============================================================
   QS Estimating Console — main.css
   PeyeX brand palette: deep navy #2B4A78 / lime green #7ED957
   ============================================================ */

/* ── CSS Custom Properties ───────────────────────────────── */
:root {
  --color-primary:      #2B4A78;   /* PeyeX navy */
  --color-primary-mid:  #3B63A0;   /* mid-blue for hover states */
  --color-accent:       #7ED957;   /* PeyeX lime green */
  --color-accent-dark:  #53A826;   /* green on white text */
  --color-success:      #1A9B6C;
  --color-warning:      #C07A0A;
  --color-danger:       #C93B28;

  /* Page backgrounds */
  --bg-primary:         #FFFFFF;
  --bg-secondary:       #FFFFFF;   /* white throughout */
  --bg-tertiary:        #FFFFFF;   /* white throughout */
  --bg-nav:             #2B4A78;

  --text-primary:       #1E293B;   /* slate-900 */
  --text-secondary:     #475569;   /* slate-600 */
  --text-hint:          #94A3B8;   /* slate-400 */

  --border-light:       rgba(0,0,0,0.07);
  --border-mid:         rgba(0,0,0,0.13);
  --border-strong:      rgba(0,0,0,0.22);

  --radius-md:          8px;
  --radius-lg:          12px;

  --font-sans:          system-ui, -apple-system, 'Inter', sans-serif;
  --font-mono:          'JetBrains Mono', 'Fira Code', monospace;

  /* Semantic tints */
  --color-success-bg:   #DCFCE7;
  --color-warning-bg:   #FEF9C3;
  --color-danger-bg:    #FFE4E6;
  --color-primary-bg:   #EEF2FF;
  --color-accent-bg:    #F0FDF4;

  /* P×I matrix */
  --pi-low-bg:   #DCFCE7; --pi-low-text:  #14532D;
  --pi-med-bg:   #FEF9C3; --pi-med-text:  #713F12;
  --pi-high-bg:  #FFEDD5; --pi-high-text: #7C2D12;
  --pi-crit-bg:  #FFE4E6; --pi-crit-text: #881337;

  /* Badges */
  --badge-green-bg:    #DCFCE7; --badge-green-text:  #14532D;
  --badge-amber-bg:    #FEF9C3; --badge-amber-text:  #713F12;
  --badge-red-bg:      #FFE4E6; --badge-red-text:    #881337;
  --badge-blue-bg:     #DBEAFE; --badge-blue-text:   #1E3A8A;
  --badge-purple-bg:   #EDE9FE; --badge-purple-text: #4C1D95;
  --badge-grey-bg:     #F1F5F9; --badge-grey-text:   #334155;

  /* Card accent colours (left border) */
  --card-accent:        var(--color-primary);

  /* Elevation */
  --shadow-sm:          0 1px 3px rgba(27,42,74,0.08), 0 1px 2px rgba(27,42,74,0.04);
  --shadow-md:          0 3px 8px rgba(27,42,74,0.10), 0 1px 3px rgba(27,42,74,0.06);
}

/* ── Dark mode ───────────────────────────────────────────── */
@media (prefers-color-scheme: dark) {
  :root {
    --bg-primary:         #1E1E2E;
    --bg-secondary:       #27273A;
    --bg-tertiary:        #313145;
    --bg-nav:             #13131F;

    --text-primary:       #E2E8F0;
    --text-secondary:     #94A3B8;
    --text-hint:          #64748B;

    --border-light:       rgba(255,255,255,0.06);
    --border-mid:         rgba(255,255,255,0.11);
    --border-strong:      rgba(255,255,255,0.20);

    --color-primary:      #7EB3F0;
    --color-primary-mid:  #5A93D8;
    --color-accent:       #7ED957;
    --color-accent-dark:  #9FE874;
    --color-success:      #2DC991;
    --color-warning:      #E09A1A;
    --color-danger:       #E36B52;

    --color-success-bg:   rgba(29,155,108,0.14);
    --color-warning-bg:   rgba(192,122,10,0.14);
    --color-danger-bg:    rgba(201,59,40,0.14);
    --color-primary-bg:   rgba(126,179,240,0.10);
    --color-accent-bg:    rgba(126,217,87,0.12);

    --badge-green-bg:     rgba(126,217,87,0.18);  --badge-green-text:  #8AE860;
    --badge-amber-bg:     rgba(230,160,30,0.20);  --badge-amber-text:  #F0C060;
    --badge-red-bg:       rgba(220,70,50,0.20);   --badge-red-text:    #F09080;
    --badge-blue-bg:      rgba(100,170,255,0.18); --badge-blue-text:   #90C8FF;
    --badge-purple-bg:    rgba(160,130,255,0.18); --badge-purple-text: #C8B8FF;
    --badge-grey-bg:      rgba(100,150,230,0.12); --badge-grey-text:   #7AADEE;

    --pi-low-bg:   rgba(126,217,87,0.20);  --pi-low-text:  #8AE860;
    --pi-med-bg:   rgba(230,160,30,0.22);  --pi-med-text:  #F0C060;
    --pi-high-bg:  rgba(220,70,50,0.22);   --pi-high-text: #F09080;
    --pi-crit-bg:  rgba(210,30,30,0.30);   --pi-crit-text: #F8A0A0;

    --shadow-sm:   0 1px 3px rgba(0,0,0,0.30);
    --shadow-md:   0 3px 8px rgba(0,0,0,0.35);
  }
}

/* ── Skip-to-content ──────────────────────────────────────── */
.skip-link {
  position: absolute;
  top: -100%;
  left: 0;
  z-index: 9999;
  background: var(--color-primary);
  color: #fff;
  padding: 0.5rem 1rem;
  font-size: 0.88rem;
  font-weight: 600;
  border-radius: 0 0 6px 0;
  text-decoration: none;
}
.skip-link:focus { top: 0; outline: 3px solid #fff; outline-offset: 2px; }

/* ── Reset & Base ─────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-size: 13px;
  line-height: 1.6;
  min-width: 768px;
}

body {
  font-family: var(--font-sans);
  font-size: 1rem;
  color: var(--text-primary);
  background: #FFFFFF;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

@media (prefers-color-scheme: dark) {
  body { background: var(--bg-primary); }
}

/* ── Navigation ───────────────────────────────────────────── */
#app-nav {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg-nav);
  border-bottom: none;
  display: flex;
  align-items: stretch;
  gap: 0;
  overflow-x: auto;
  scrollbar-width: none;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25);
}

#app-nav::-webkit-scrollbar { display: none; }

#app-nav::after {
  content: '';
  position: sticky;
  right: 0;
  top: 0;
  height: 100%;
  width: 2.5rem;
  flex-shrink: 0;
  background: linear-gradient(to right, transparent, var(--bg-nav));
  pointer-events: none;
}

.nav-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0 1.1rem;
  font-weight: 700;
  font-size: 0.95rem;
  color: #ffffff;
  white-space: nowrap;
  border-right: 1px solid rgba(255,255,255,0.12);
  flex-shrink: 0;
  letter-spacing: 0.01em;
}

.nav-brand img {
  width: 24px;
  height: 24px;
  border-radius: 5px;
}

/* Green accent dot after brand name */
.nav-brand::after {
  content: '●';
  font-size: 0.5rem;
  color: var(--color-accent);
  line-height: 1;
  align-self: center;
  margin-left: 0.1rem;
}

.nav-tabs {
  display: flex;
  align-items: stretch;
  flex: 1;
  overflow-x: auto;
  scrollbar-width: none;
}

.nav-tabs::-webkit-scrollbar { display: none; }

.nav-tab {
  display: flex;
  align-items: center;
  padding: 0 1rem;
  height: 44px;
  font-size: 0.82rem;
  font-weight: 400;
  color: rgba(255,255,255,0.60);
  white-space: nowrap;
  cursor: pointer;
  border: none;
  background: none;
  border-bottom: 3px solid transparent;
  transition: color 0.15s, background 0.15s, border-color 0.15s;
  position: relative;
}

.nav-tab:hover {
  color: rgba(255,255,255,0.90);
  background: rgba(255,255,255,0.07);
}

.nav-tab.active {
  color: #ffffff;
  border-bottom-color: var(--color-accent);
  font-weight: 600;
  background: rgba(255,255,255,0.06);
}

.nav-tab:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: -2px;
}

/* Nav right cluster */
.nav-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0 0.75rem;
  margin-left: auto;
  flex-shrink: 0;
  border-left: 1px solid rgba(255,255,255,0.12);
}

#btn-sync {
  display: flex;
  align-items: center;
  font-size: 0.78rem;
  font-weight: 600;
  color: rgba(255,255,255,0.65);
  background: transparent;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: var(--radius-md);
  padding: 0.3rem 0.7rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
  max-width: 14rem;
  overflow: hidden;
  text-overflow: ellipsis;
}
#btn-sync:hover { background: rgba(255,255,255,0.08); color: #fff; }
#btn-sync:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
#btn-sync.synced { color: var(--color-accent); border-color: rgba(126,217,87,0.35); }

#btn-install {
  display: none;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--color-accent);
  background: rgba(126,217,87,0.12);
  border: 1px solid rgba(126,217,87,0.35);
  border-radius: var(--radius-md);
  padding: 0.3rem 0.7rem;
  cursor: pointer;
  transition: background 0.15s;
}
#btn-install:hover { background: rgba(126,217,87,0.20); }
#btn-install.visible { display: flex; }
#btn-install:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }

#save-indicator {
  font-size: 0.72rem;
  color: rgba(255,255,255,0.45);
  min-width: 4rem;
  text-align: right;
}

/* ── Banners ──────────────────────────────────────────────── */
.banner {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.5rem 1.25rem;
  font-size: 0.82rem;
  border-bottom: 0.5px solid var(--border-light);
}
.banner.visible { display: flex; }

#banner-offline {
  background: var(--badge-amber-bg);
  color: var(--badge-amber-text);
}
#banner-update {
  background: var(--badge-blue-bg);
  color: var(--badge-blue-text);
}
.banner button {
  font-size: 0.8rem;
  font-weight: 500;
  padding: 0.25rem 0.65rem;
  border-radius: var(--radius-md);
  border: 0.5px solid currentColor;
  background: none;
  cursor: pointer;
  color: inherit;
}
.banner button:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; }

/* ── Main content area ────────────────────────────────────── */
#app-content {
  flex: 1;
  padding: 1.25rem 1.5rem;
  overflow: auto;
}

.module-panel {
  display: none;
  animation: fadein 0.18s ease;
}
.module-panel.active { display: block; }

@keyframes fadein {
  from { opacity: 0; transform: translateY(5px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Cards ────────────────────────────────────────────────── */
.card {
  background: var(--bg-primary);
  border: 0.5px solid var(--border-mid);
  border-left: 3px solid var(--color-primary);
  border-radius: var(--radius-lg);
  padding: 1rem 1.25rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow-sm);
}

.card-title {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--color-primary);
  margin-bottom: 0.75rem;
  padding-bottom: 0.5rem;
  border-bottom: 0.5px solid var(--border-light);
  letter-spacing: 0.01em;
}

/* ── Metric cards grid ────────────────────────────────────── */
.metric-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.metric-card {
  background: var(--bg-secondary);    /* light brand-blue tint */
  border: 0.5px solid var(--border-mid);
  border-top: 3px solid var(--color-primary);
  border-radius: var(--radius-md);
  padding: 0.75rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  box-shadow: var(--shadow-sm);
}

.metric-card .label {
  font-size: 0.70rem;
  color: var(--text-hint);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
}

.metric-card .value {
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text-primary);
}

.metric-card .sub {
  font-size: 0.72rem;
  color: var(--text-hint);
}

/* ── Metric card colours — shuffled, no obvious repeat ────── */
/* Sequence (T=teal, Y=yellow, L=lavender, P=pink):
   1 T  2 Y  3 L  4 P  5 Y  6 L  7 T  8 P
   9 Y  10 T  11 P  12 L
   13+ falls back to a different-offset 4-cycle so it never
   aligns with the opening run */

.metric-grid .metric-card:nth-child(1)  { background:#EDFAF8; border-top-color:#00A892; }
.metric-grid .metric-card:nth-child(2)  { background:#FCFCDE; border-top-color:#A8A800; }
.metric-grid .metric-card:nth-child(3)  { background:#F0F0FC; border-top-color:#5858C0; }
.metric-grid .metric-card:nth-child(4)  { background:#FCF0FC; border-top-color:#B040B0; }
.metric-grid .metric-card:nth-child(5)  { background:#FCFCDE; border-top-color:#A8A800; }
.metric-grid .metric-card:nth-child(6)  { background:#F0F0FC; border-top-color:#5858C0; }
.metric-grid .metric-card:nth-child(7)  { background:#EDFAF8; border-top-color:#00A892; }
.metric-grid .metric-card:nth-child(8)  { background:#FCF0FC; border-top-color:#B040B0; }
.metric-grid .metric-card:nth-child(9)  { background:#FCFCDE; border-top-color:#A8A800; }
.metric-grid .metric-card:nth-child(10) { background:#EDFAF8; border-top-color:#00A892; }
.metric-grid .metric-card:nth-child(11) { background:#FCF0FC; border-top-color:#B040B0; }
.metric-grid .metric-card:nth-child(12) { background:#F0F0FC; border-top-color:#5858C0; }
/* 13+: offset cycle — starts on lavender so it doesn't echo position 1 */
.metric-grid .metric-card:nth-child(n+13):nth-child(4n+1) { background:#F0F0FC; border-top-color:#5858C0; }
.metric-grid .metric-card:nth-child(n+13):nth-child(4n+2) { background:#EDFAF8; border-top-color:#00A892; }
.metric-grid .metric-card:nth-child(n+13):nth-child(4n+3) { background:#FCF0FC; border-top-color:#B040B0; }
.metric-grid .metric-card:nth-child(n+13):nth-child(4n+0) { background:#FCFCDE; border-top-color:#A8A800; }

@media (prefers-color-scheme: dark) {
  .metric-grid .metric-card:nth-child(1),
  .metric-grid .metric-card:nth-child(7),
  .metric-grid .metric-card:nth-child(10),
  .metric-grid .metric-card:nth-child(n+13):nth-child(4n+2)
    { background:rgba(0,245,212,0.14); border-top-color:#EDFAF8; }

  .metric-grid .metric-card:nth-child(2),
  .metric-grid .metric-card:nth-child(5),
  .metric-grid .metric-card:nth-child(9),
  .metric-grid .metric-card:nth-child(n+13):nth-child(4n+3)
    { background:rgba(168,168,0,0.13); border-top-color:#D8D840; }

  .metric-grid .metric-card:nth-child(3),
  .metric-grid .metric-card:nth-child(6),
  .metric-grid .metric-card:nth-child(12),
  .metric-grid .metric-card:nth-child(n+13):nth-child(4n+1)
    { background:rgba(88,88,192,0.16); border-top-color:#9898E8; }

  .metric-grid .metric-card:nth-child(4),
  .metric-grid .metric-card:nth-child(8),
  .metric-grid .metric-card:nth-child(11),
  .metric-grid .metric-card:nth-child(n+13):nth-child(4n+0)
    { background:rgba(176,64,176,0.14); border-top-color:#E07AE0; }
}

/* Accent metric card (success/all-in rate) */
.metric-card[style*="success"] {
  border-top-color: var(--color-accent-dark);
}

/* ── Badges ───────────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.18em 0.6em;
  border-radius: 5px;
  font-size: 0.70rem;
  font-weight: 600;
  white-space: nowrap;
  letter-spacing: 0.02em;
}

.badge-green  { background: var(--badge-green-bg);  color: var(--badge-green-text); }
.badge-amber  { background: var(--badge-amber-bg);  color: var(--badge-amber-text); }
.badge-red    { background: var(--badge-red-bg);    color: var(--badge-red-text); }
.badge-blue   { background: var(--badge-blue-bg);   color: var(--badge-blue-text); }
.badge-purple { background: var(--badge-purple-bg); color: var(--badge-purple-text); }
.badge-grey   { background: var(--badge-grey-bg);   color: var(--badge-grey-text); }

/* ── Tables ───────────────────────────────────────────────── */
.table-wrap {
  overflow-x: auto;
  border: 0.5px solid var(--border-mid);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

th {
  font-size: 0.70rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-primary);
  background: #FFFFFF;
  padding: 0.6rem 0.75rem;
  border-bottom: 1.5px solid var(--border-mid);
  text-align: left;
  white-space: nowrap;
}

@media (prefers-color-scheme: dark) {
  th { background: var(--bg-tertiary); }
}

th.num, td.num { text-align: right; }

td {
  padding: 0.5rem 0.75rem;
  border-bottom: 0.5px solid var(--border-light);
  color: var(--text-primary);
  vertical-align: middle;
}

tr:last-child td { border-bottom: none; }

tbody tr:nth-child(even) td { background: #FFFFFF; }

tbody tr:hover td {
  background: var(--color-primary-bg) !important;
}

tfoot td {
  font-weight: 600;
  background: var(--bg-tertiary);
  border-top: 1.5px solid var(--border-mid);
  color: var(--color-primary);
}

/* ── Form elements ────────────────────────────────────────── */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 1rem 1.5rem;
  margin-bottom: 1rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  letter-spacing: 0.01em;
}

input[type="text"],
input[type="number"],
input[type="month"],
select,
textarea {
  font-family: var(--font-sans);
  font-size: 0.88rem;
  color: var(--text-primary);
  background: var(--bg-primary);
  border: 1px solid var(--border-mid);
  border-radius: var(--radius-md);
  padding: 0.45rem 0.65rem;
  width: 100%;
  transition: border-color 0.15s, box-shadow 0.15s;
}

input[type="number"] { font-family: var(--font-mono); text-align: right; }

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(43,74,120,0.12);
}

input.invalid, select.invalid {
  border-color: var(--color-danger);
  box-shadow: 0 0 0 3px rgba(201,59,40,0.10);
}

.field-error {
  font-size: 0.72rem;
  color: var(--color-danger);
  margin-top: 0.15rem;
}

.helper-text {
  font-size: 0.72rem;
  color: var(--text-hint);
}

/* Inline number input inside table */
td input[type="number"] {
  border: 0.5px solid var(--border-light);
  border-radius: 4px;
  padding: 0.25rem 0.4rem;
  width: 100%;
  min-width: 80px;
  background: transparent;
  font-size: 0.85rem;
  text-align: right;
}

td input[type="number"]:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 2px rgba(43,74,120,0.12);
  background: var(--bg-primary);
}

/* Range slider */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 5px;
  border-radius: 3px;
  background: var(--border-mid);
  border: none;
  padding: 0;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 17px;
  height: 17px;
  border-radius: 50%;
  background: var(--color-primary);
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0,0,0,0.20);
}
input[type="range"]:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 4px;
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.slider-value {
  font-size: 0.85rem;
  font-weight: 600;
  min-width: 3rem;
  text-align: right;
  font-family: var(--font-mono);
  color: var(--color-primary);
}

/* ── Buttons ──────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-family: var(--font-sans);
  font-size: 0.82rem;
  font-weight: 600;
  padding: 0.4rem 0.9rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-mid);
  background: var(--bg-primary);
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, color 0.12s, box-shadow 0.12s;
  box-shadow: var(--shadow-sm);
}
.btn:hover {
  background: var(--bg-secondary);
  border-color: var(--border-strong);
  color: var(--text-primary);
}
.btn:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 2px; }

.btn-primary {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
}
.btn-primary:hover {
  background: var(--color-primary-mid);
  border-color: var(--color-primary-mid);
  color: #fff;
}

.btn-accent {
  background: var(--color-accent);
  border-color: var(--color-accent-dark);
  color: #1B2A4A;
}
.btn-accent:hover { opacity: 0.88; background: var(--color-accent); }

.btn-danger {
  color: var(--color-danger);
  border-color: rgba(201,59,40,0.30);
  background: transparent;
  box-shadow: none;
}
.btn-danger:hover { background: var(--badge-red-bg); border-color: var(--color-danger); }

.btn-sm { font-size: 0.73rem; padding: 0.22rem 0.6rem; }

/* ── Toggle group ─────────────────────────────────────────── */
.toggle-group {
  display: flex;
  border: 1px solid var(--border-mid);
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.toggle-btn {
  flex: 1;
  padding: 0.4rem 0.65rem;
  font-size: 0.80rem;
  font-weight: 500;
  border: none;
  border-right: 0.5px solid var(--border-mid);
  background: var(--bg-primary);
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
  text-align: center;
}
.toggle-btn:last-child { border-right: none; }
.toggle-btn.active {
  background: var(--color-primary);
  color: #fff;
  font-weight: 600;
}
.toggle-btn:focus-visible { outline: 2px solid var(--color-primary); outline-offset: -2px; }

/* ── Sub-tab bar (inside modules e.g. Rate Library) ───────── */
.tab-bar {
  display: flex;
  gap: 0;
  border-bottom: 2px solid var(--border-mid);
  margin-bottom: 1rem;
}
.tab-btn {
  padding: 0.5rem 1.1rem;
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text-secondary);
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  margin-bottom: -2px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}
.tab-btn:hover { color: var(--color-primary); }
.tab-btn.active {
  color: var(--color-primary);
  border-bottom-color: var(--color-primary);
  font-weight: 700;
}
.tab-btn:focus-visible { outline: 2px solid var(--color-primary); outline-offset: -2px; }

/* ── Charts ───────────────────────────────────────────────── */
.chart-wrap {
  position: relative;
  height: 300px;
  margin: 0.5rem 0;
}
.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
}
.chart-legend-item {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}
.legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  flex-shrink: 0;
}

/* ── Section header ───────────────────────────────────────── */
.section-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
  padding-bottom: 0.6rem;
  border-bottom: 2px solid var(--border-light);
}
.section-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--color-primary);
  letter-spacing: 0.01em;
}

/* ── VAT toggle ───────────────────────────────────────────── */
.vat-toggle { display: flex; align-items: center; gap: 0.5rem; }
.vat-toggle .toggle-group { width: auto; }
.vat-toggle .toggle-btn { padding: 0.25rem 0.65rem; font-size: 0.78rem; }

/* ── Cost summary build-up table ──────────────────────────── */
.buildoup-table {
  width: 100%;
  font-size: 0.88rem;
  border-collapse: collapse;
}
.buildoup-table tr td {
  padding: 0.4rem 0.75rem;
  border-bottom: 0.5px solid var(--border-light);
}
.buildoup-table tr.subtotal td {
  font-weight: 600;
  background: var(--bg-secondary);
  border-top: 0.5px solid var(--border-mid);
  color: var(--color-primary);
}
.buildoup-table tr.total td {
  font-weight: 700;
  font-size: 1rem;
  background: var(--bg-tertiary);
  border-top: 1.5px solid var(--border-mid);
  color: var(--color-primary);
}
.buildoup-table td.num { text-align: right; font-family: var(--font-mono); }

/* ── Utility ──────────────────────────────────────────────── */
.mono { font-family: var(--font-mono); }
.text-right { text-align: right; }
.text-muted { color: var(--text-secondary); }
.text-hint { color: var(--text-hint); font-size: 0.78rem; }
.text-danger { color: var(--color-danger); }
.text-success { color: var(--color-success); }
.text-warning { color: var(--color-warning); }

.mt-1 { margin-top: 0.5rem; }
.mt-2 { margin-top: 1rem; }
.mt-3 { margin-top: 1.5rem; }
.mb-1 { margin-bottom: 0.5rem; }
.mb-2 { margin-bottom: 1rem; }

.flex { display: flex; }
.flex-center { display: flex; align-items: center; }
.gap-1 { gap: 0.5rem; }
.gap-2 { gap: 1rem; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* ── Rate library build-up ────────────────────────────────── */
.bu-section-summary {
  font-weight: 600;
  cursor: pointer;
  padding: 0.5rem 0;
  border-bottom: 0.5px solid var(--border-light);
  margin-bottom: 0.75rem;
  list-style: none;
  color: var(--color-primary);
}
.bu-section-summary::marker,
.bu-section-summary::-webkit-details-marker { display: none; }
.bu-section-summary::before { content: '▸  '; font-size: 0.75em; color: var(--color-accent-dark); }
details[open] > .bu-section-summary::before { content: '▾  '; }

.bu-form-grid { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.bu-derived {
  font-size: 0.95rem;
  font-family: var(--font-mono);
  padding: 0.3rem 0;
  color: var(--color-primary);
  font-weight: 600;
}

/* ── Confirm dialog ───────────────────────────────────────── */
.dialog-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(10,20,50,0.55);
  z-index: 500;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(2px);
}
.dialog-backdrop.open { display: flex; }

.dialog {
  background: var(--bg-primary);
  border: 0.5px solid var(--border-strong);
  border-top: 3px solid var(--color-primary);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  max-width: 420px;
  width: 90%;
  box-shadow: var(--shadow-md);
}
.dialog-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--color-primary);
  margin-bottom: 0.5rem;
}
.dialog-body {
  font-size: 0.88rem;
  color: var(--text-secondary);
  margin-bottom: 1.25rem;
}
/* ── Project picker ─────────────────────────────────────────────────────── */
.picker-dialog { min-width: 30rem; max-width: 90vw; }
.picker-list { max-height: 45vh; overflow-y: auto; margin-bottom: 1rem; }
.picker-row {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  width: 100%;
  text-align: left;
  padding: 0.6rem 0.75rem;
  margin-bottom: 0.4rem;
  border: 1px solid var(--border, #d8dee6);
  border-radius: var(--radius-md);
  background: #fff;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.picker-row:hover { background: #f2f6fb; border-color: var(--color-primary); }
.picker-row:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 1px; }
.picker-row-name { font-weight: 600; color: var(--color-primary); }
.picker-row-meta { font-size: 0.78rem; color: var(--text-secondary); }
.picker-empty { font-size: 0.85rem; color: var(--text-secondary); padding: 0.75rem 0; }
.picker-new { border-top: 1px solid var(--border, #d8dee6); padding-top: 0.9rem; margin-bottom: 1rem; }
.picker-new-title { font-size: 0.8rem; font-weight: 600; color: var(--text-secondary); margin-bottom: 0.5rem; }

#btn-projects {
  display: flex;
  align-items: center;
  font-size: 0.78rem;
  font-weight: 600;
  color: rgba(255,255,255,0.65);
  background: transparent;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: var(--radius-md);
  padding: 0.3rem 0.7rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
}
#btn-projects:hover { background: rgba(255,255,255,0.08); color: #fff; }
#btn-projects:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }

/* ── Admin (Module 13) ──────────────────────────────────────────────────── */
.admin-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.admin-table th, .admin-table td {
  padding: 0.45rem 0.6rem;
  border-bottom: 1px solid var(--border, #d8dee6);
  text-align: left;
  vertical-align: middle;
}
.admin-table th { font-weight: 600; color: var(--text-secondary); font-size: 0.75rem; text-transform: uppercase; }

/* ── Viewer read-only mode ──────────────────────────────────────────────── */
body.readonly #app-content :is(input, select, textarea) {
  pointer-events: none;
  background: #eef1f4;
  color: var(--text-secondary);
}
body.readonly #app-content button:not(.allow-readonly) {
  pointer-events: none;
  opacity: 0.45;
}
body.readonly::after {
  content: 'View only';
  position: fixed;
  bottom: 0.75rem;
  right: 0.75rem;
  z-index: 500;
  background: var(--color-primary);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.3rem 0.7rem;
  border-radius: var(--radius-md);
  opacity: 0.9;
  pointer-events: none;
}

.cloud-field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin-bottom: 1rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
}
.cloud-input {
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  background: var(--bg-input, #fff);
  color: var(--text-primary);
  width: 100%;
  box-sizing: border-box;
}
.cloud-input:focus { outline: 2px solid var(--color-primary); outline-offset: 1px; }

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

/* ── Progress bars ────────────────────────────────────────── */
.progress-wrap {
  background: var(--bg-tertiary);
  border-radius: 4px;
  height: 8px;
  overflow: hidden;
  margin: 0.25rem 0;
}
.progress-bar {
  height: 100%;
  border-radius: 4px;
  transition: width 0.3s ease;
  background: linear-gradient(90deg, var(--color-primary), var(--color-primary-mid));
}

/* ── P×I matrix ───────────────────────────────────────────── */
.pi-matrix {
  display: grid;
  grid-template-columns: auto repeat(4, 1fr);
  gap: 3px;
  font-size: 0.78rem;
}
.pi-cell {
  border-radius: 5px;
  padding: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 52px;
  font-weight: 600;
  cursor: default;
  position: relative;
}
.pi-cell.score-low  { background: var(--pi-low-bg,  #D6F5E8); color: var(--pi-low-text,  #0A5238); }
.pi-cell.score-med  { background: var(--pi-med-bg,  #FEF3DC); color: var(--pi-med-text,  #6B3E00); }
.pi-cell.score-high { background: var(--pi-high-bg, #FCEAE6); color: var(--pi-high-text, #7A1F0F); }
.pi-cell.score-crit { background: var(--pi-crit-bg, #F8DADA); color: var(--pi-crit-text, #6B0F0F); font-weight: 700; }
.pi-cell.header     { background: none; font-weight: 600; color: var(--text-secondary); font-size: 0.72rem; }

/* ── Benchmark bars ───────────────────────────────────────── */
.benchmark-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
  font-size: 0.8rem;
}
.benchmark-label {
  min-width: 180px;
  color: var(--text-secondary);
  font-size: 0.75rem;
}
.benchmark-track {
  flex: 1;
  position: relative;
  height: 16px;
  background: var(--bg-tertiary);
  border-radius: 8px;
  overflow: visible;
}
.benchmark-band {
  position: absolute;
  height: 100%;
  background: var(--badge-blue-bg);
  border-radius: 8px;
}
.benchmark-dot {
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 11px;
  height: 11px;
  border-radius: 50%;
  border: 2px solid var(--bg-primary);
  box-shadow: 0 1px 3px rgba(0,0,0,0.20);
}
.benchmark-value {
  min-width: 70px;
  text-align: right;
  font-family: var(--font-mono);
  font-size: 0.78rem;
}

/* ── Prompt buttons ───────────────────────────────────────── */
.prompt-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 1rem;
}

canvas[role="img"] { display: block; }

.summary-two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  align-items: start;
}

/* ============================================================
   TABLET LAYOUT  (768px – 1199px)
   Target: iPad landscape (1024px) and portrait (768px)
   Touch-friendly: larger tap targets, scroll hints
   ============================================================ */

/* ── Shared tablet rules (landscape + portrait) ─────────────── */
@media screen and (min-width: 768px) and (max-width: 1199px) {

  /* Nav bar — allow wrapping so tabs can break to second row */
  #app-nav {
    flex-wrap: wrap;
    height: auto;
    padding: 0;
  }

  .nav-brand {
    flex: 1 1 auto;
    padding: 0.6rem 1rem;
    min-height: 44px;
  }

  .nav-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.4rem 1rem;
    flex: 0 0 auto;
  }

  /* Tab strip scrolls horizontally on tablet */
  .nav-tabs {
    flex: 0 0 100%;
    order: 2;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    border-top: 1px solid rgba(255,255,255,0.12);
    padding: 0 0.5rem;
    gap: 0;
  }
  .nav-tabs::-webkit-scrollbar { display: none; }

  .nav-tab {
    flex: 0 0 auto;
    height: 40px;
    padding: 0 0.85rem;
    font-size: 0.8rem;
    white-space: nowrap;
  }

  /* Content area — tighter side padding */
  #app-content {
    padding: 1rem;
  }

  /* Form grid — 2 columns on tablet landscape */
  .form-grid {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  }

  /* Metric cards — 3 per row on tablet */
  .metric-grid {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  }

  /* Summary two-col → single col */
  .summary-two-col {
    grid-template-columns: 1fr;
  }

  /* All tables scroll horizontally — touch swipe friendly */
  .table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Bigger touch targets for inputs and selects */
  input[type="text"],
  input[type="number"],
  input[type="date"],
  select,
  textarea {
    min-height: 38px;
    font-size: 0.9rem;
  }

  /* Buttons — taller for touch */
  .btn {
    min-height: 38px;
    padding: 0.45rem 0.9rem;
  }
  .btn-sm {
    min-height: 32px;
    padding: 0.3rem 0.65rem;
    font-size: 0.78rem;
  }

  /* Section headers */
  .section-title {
    font-size: 0.9rem;
  }

  /* Timber / rates table action columns stay usable */
  .rates-table td,
  .rates-table th {
    padding: 0.45rem 0.4rem;
  }
}

/* ── Landscape sub-range (901px – 1199px) ───────────────────── */
@media screen and (min-width: 901px) and (max-width: 1199px) {
  html { font-size: 14px; }
}

/* ── Portrait sub-range (768px – 900px) ─────────────────────── */
@media screen and (min-width: 768px) and (max-width: 900px) {

  /* Single column form grid in portrait */
  .form-grid {
    grid-template-columns: 1fr;
  }

  /* Metric grid — 2 per row */
  .metric-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  /* Reduce font to reclaim space */
  html {
    font-size: 13px;
  }

  /* Nav brand + actions on one row, tabs on second row — already handled
     by flex-wrap above; just tighten the brand padding */
  .nav-brand {
    padding: 0.5rem 0.75rem;
    font-size: 0.85rem;
  }

  /* Modal dialogs — full width on portrait tablet */
  .modal-dialog {
    width: 92vw !important;
    max-width: none !important;
  }
}
