/* ==========================================================================
   Canadian Metrology Reference Suite — Unified Stylesheet
   ========================================================================== */

:root {
  /* Surface and Neutral Tokens */
  --bg-app: #f4f4f2;
  --bg-card: #ffffff;
  --bg-subtle: #eeede9;
  --text-main: #18181b;
  --text-muted: #64748b;
  --text-hint: #94a3b8;
  --border-color: #dcdcd6;
  --border-focus: #1e293b;

  /* Regulatory Verdict Tokens */
  --pass-bg: #eaf5ea;
  --pass-text: #1b6329;
  --fail-bg: #fdeeee;
  --fail-text: #991b1b;
  --warn-text: #854d0e;

  /* Tool-Specific Accent (Overridden per tool) */
  --tool-accent: #1e293b;
  --tool-accent-bg: #f1f5f9;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --font-stack:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-app: #121214;
    --bg-card: #1c1c1f;
    --bg-subtle: #26262b;
    --text-main: #f4f4f5;
    --text-muted: #9ca3af;
    --text-hint: #6b7280;
    --border-color: #323238;
    --border-focus: #94a3b8;

    --pass-bg: #143319;
    --pass-text: #86efac;
    --fail-bg: #3b1414;
    --fail-text: #fca5a5;
    --warn-text: #fde047;

    --tool-accent: #94a3b8;
    --tool-accent-bg: #27272a;
  }
}

/* Tool Accent Presets */
[data-theme="about"] {
  --tool-accent: #64748b;
  --tool-accent-bg: #f1f5f9;
}
[data-theme="pint"] {
  --tool-accent: #b45309;
  --tool-accent-bg: #fef3c7;
}
[data-theme="scales"] {
  --tool-accent: #4f46e5;
  --tool-accent-bg: #eef2ff;
}
[data-theme="fabric"] {
  --tool-accent: #15803d;
  --tool-accent-bg: #dcfce7;
}
[data-theme="linear"] {
  --tool-accent: #0284c7;
  --tool-accent-bg: #e0f2fe;
}

/* Reset & Base */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
body {
  font-family: var(--font-stack);
  background: var(--bg-app);
  color: var(--text-main);
  min-height: 100vh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* Layout Shell */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border-color);
  height: 54px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
}
.app-header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}
.header-titles h1 {
  font-size: 15px;
  font-weight: 600;
  line-height: 1.2;
}
.header-titles p {
  font-size: 11px;
  color: var(--text-muted);
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-main);
  font-size: 20px;
  cursor: pointer;
  padding: 8px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
}
.icon-btn:hover {
  background: var(--bg-subtle);
}

.app-viewport {
  max-width: 520px;
  margin: 0 auto;
  padding: 1rem 1rem 3rem;
}

/* Slide-out Drawer */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.drawer-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: 290px;
  max-width: 85vw;
  background: var(--bg-card);
  z-index: 201;
  transform: translateX(-100%);
  transition: transform 0.25s ease-out;
  display: flex;
  flex-direction: column;
  box-shadow: 4px 0 24px rgba(0, 0, 0, 0.15);
}
.drawer.open {
  transform: translateX(0);
}

.drawer-header {
  padding: 1.25rem 1rem;
  border-bottom: 1px solid var(--border-color);
}
.drawer-header h2 {
  font-size: 16px;
  font-weight: 600;
}
.drawer-header p {
  font-size: 12px;
  color: var(--text-muted);
}

.drawer-nav {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem 0.5rem;
}

/* Drawer Section Dividers */
.nav-section-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-hint);
  padding: 14px 12px 6px 12px;
  margin-top: 6px;
  border-top: 1px solid var(--border-color);
}
.drawer-nav .nav-section-label:first-child {
  border-top: none;
  margin-top: 0;
  padding-top: 4px;
}

.drawer-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 12px;
  margin-bottom: 4px;
  border-radius: var(--radius-md);
  border: none;
  background: none;
  color: var(--text-main);
  font-family: inherit;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
}
.drawer-item:hover {
  background: var(--bg-subtle);
}
.drawer-item.active {
  background: var(--tool-accent-bg);
  font-weight: 600;
}
.drawer-item .item-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--item-color, var(--text-muted));
  flex-shrink: 0;
}
.drawer-item .item-sub {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 400;
}

.drawer-footer {
  padding: 1rem;
  border-top: 1px solid var(--border-color);
  font-size: 11px;
  color: var(--text-muted);
}

/* Card System */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 1rem 1.125rem;
  margin-bottom: 0.875rem;
}

.card-header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 12px;
  margin-bottom: 10px;
}
.card-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-muted);
  flex-shrink: 0;
}

/* Inputs & Form Elements */
.field {
  margin-bottom: 10px;
}
.field:last-child {
  margin-bottom: 0;
}
.field-label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.input-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

input[type="number"],
input[type="text"] {
  flex: 1;
  min-width: 0;
  height: 44px;
  padding: 0 12px;
  font-size: 15px;
  font-family: inherit;
  background: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-main);
  outline: none;
  transition: border-color 0.15s;
}
input:focus {
  border-color: var(--tool-accent);
}
.field-unit {
  font-size: 13px;
  color: var(--text-muted);
  flex-shrink: 0;
}
.field-err {
  font-size: 12px;
  color: var(--fail-text);
  margin-top: 4px;
  min-height: 16px;
}

.field-conv {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 6px;
  font-variant-numeric: tabular-nums;
  display: flex;
  align-items: center;
  gap: 4px;
}
.field-conv:empty {
  display: none;
}

/* Enhanced Segmented Toggles */
.toggle-group {
  display: inline-flex;
  align-items: center;
  background: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 3px;
  flex-shrink: 0;
}
.toggle-btn {
  padding: 5px 14px;
  font-size: 13px;
  font-weight: 500;
  background: none;
  border: none;
  color: var(--text-muted);
  border-radius: calc(var(--radius-sm) - 2px);
  cursor: pointer;
  min-height: 32px;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition:
    background 0.15s,
    color 0.15s,
    box-shadow 0.15s;
}
.toggle-btn.active {
  background: var(--tool-accent);
  color: #ffffff !important;
  font-weight: 600;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.18);
}

/* Action Bar */
.action-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 0.875rem;
}
.btn-clear {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 6px 14px;
  font-family: inherit;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
}
.btn-clear:hover {
  background: var(--bg-subtle);
  color: var(--text-main);
  border-color: var(--text-muted);
}
.btn-clear:active {
  transform: scale(0.97);
}

.btn-primary {
  width: 100%;
  height: 46px;
  font-size: 15px;
  font-weight: 600;
  background: var(--tool-accent);
  color: #ffffff;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: opacity 0.15s;
}
.btn-primary:active {
  opacity: 0.85;
}

/* Verdict Badges */
.verdict-banner {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}
.verdict-badge {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  letter-spacing: 0.03em;
  white-space: nowrap;
}
.badge-pass {
  background: var(--pass-bg);
  color: var(--pass-text);
}
.badge-fail {
  background: var(--fail-bg);
  color: var(--fail-text);
}
.badge-exact {
  background: var(--bg-subtle);
  color: var(--text-muted);
}

/* System Badges */
.sys-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 4px;
  letter-spacing: 0.02em;
}
.sys-metric {
  background: #e0f2fe;
  color: #0369a1;
}
.sys-canadian {
  background: #fef3c7;
  color: #92400e;
}

@media (prefers-color-scheme: dark) {
  .sys-metric {
    background: #082f49;
    color: #7dd3fc;
  }
  .sys-canadian {
    background: #451a03;
    color: #fde68a;
  }
}

/* Comparative Results Table */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
table.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
table.data-table th,
table.data-table td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--border-color);
  text-align: right;
  vertical-align: middle;
}
table.data-table th:first-child,
table.data-table td:first-child {
  text-align: left;
}
table.data-table th {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  background: var(--bg-subtle);
}

table.data-table tr.rule-row td {
  padding: 2px 10px 8px;
  border-bottom: 1px solid var(--border-color);
  text-align: left;
}
.rule-text {
  font-size: 11px;
  color: var(--text-hint);
  font-style: italic;
  line-height: 1.4;
}

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

/* Tolerance Percent Formatting */
.val-pass {
  color: var(--pass-text);
  font-weight: 600;
}
.val-warn {
  color: var(--warn-text);
  font-weight: 600;
}
.val-fail {
  color: var(--fail-text);
  font-weight: 600;
}

/* Native Collapsible for Regulation Texts */
details.reg-details {
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 10px 12px;
  background: var(--bg-card);
  margin-top: 1rem;
}
details.reg-details summary {
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  list-style: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--text-muted);
}
details.reg-details summary::-webkit-details-marker {
  display: none;
}
details.reg-details summary::after {
  content: "▼";
  font-size: 10px;
  transition: transform 0.2s;
}
details.reg-details[open] summary::after {
  transform: rotate(180deg);
}
.reg-body {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border-color);
  font-size: 12px;
  line-height: 1.6;
  color: var(--text-main);
}
.reg-body p {
  margin-bottom: 8px;
}

.hidden {
  display: none !important;
}
