:root {
  --white: #FFFFFF;
  --red: #FF0000;
  --gold: #DDC592;
  --gray: #D9D9D9;
  --paper: #F2F2F2;
  --ink: #1A1A1A;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

:root, [data-theme="light"] {
  --bg: var(--paper);
  --surface: var(--white);
  --text: var(--ink);
  --muted: color-mix(in srgb, var(--ink) 62%, var(--paper));
  --line: var(--gray);
  --accent: var(--gold);
  --danger: var(--red);
  color-scheme: light;
}

[data-theme="dark"] {
  --bg: var(--ink);
  --surface: color-mix(in srgb, var(--ink) 90%, var(--white));
  --text: var(--paper);
  --muted: color-mix(in srgb, var(--paper) 62%, var(--ink));
  --line: color-mix(in srgb, var(--ink) 72%, var(--white));
  --accent: var(--gold);
  --danger: var(--red);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html {
  font-size: 16px;
  width: 100%;
  overflow-x: clip;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  width: 100%;
  min-height: 100dvh;
  overflow-x: clip;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.4;
  padding-bottom: calc(16px + var(--safe-b));
  overscroll-behavior-y: contain;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

h1, h2 { margin: 0; line-height: 1.2; }
h2 { font-size: .95rem; margin-bottom: 8px; display: flex; align-items: center; gap: 8px; }
p { margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: .8rem; }
.center { text-align: center; }

/* Splash */
#splash {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center;
  background: var(--gold);
  transition: opacity .4s ease, visibility .4s;
}
#splash img { width: min(60vw, 280px); }
#splash.hide { opacity: 0; visibility: hidden; }

/* Cabeçalho compacto */
.app-header {
  width: 100%;
  background: var(--gold);
  color: var(--ink);
  padding: calc(6px + var(--safe-t)) 12px 6px;
}
.brand { display: flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; }
.brand h1 { font-size: 1.1rem; font-weight: 800; letter-spacing: .01em; }

.toolbar {
  position: sticky; top: 0; z-index: 20;
  width: 100%;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 2px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.toolbar-right { display: flex; align-items: center; gap: 2px; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 44px; height: 44px;
  border: 0; border-radius: 12px;
  background: transparent; color: var(--text);
  cursor: pointer; text-decoration: none;
}
.icon-btn:active { background: var(--line); }

/* Botões */
.btn {
  min-height: 44px; padding: 0 10px;
  border-radius: 12px;
  border: 2px solid var(--line);
  background: var(--surface); color: var(--text);
  font: inherit; font-size: .88rem; font-weight: 600;
  cursor: pointer;
}
.btn:active:not(:disabled) { transform: scale(.97); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--ink); }
.btn-ghost { border-color: transparent; background: transparent; font-size: 1rem; padding: 0 12px; }
.btn-sq { width: 48px; min-height: 48px; padding: 0; font-size: 1.3rem; flex: none; }
:focus-visible { outline: 3px solid var(--danger); outline-offset: 2px; }

/* Menu */
.menu-wrap { position: relative; }
.menu {
  position: absolute; top: calc(100% + 6px); left: 0;
  min-width: 270px; max-width: calc(100vw - 16px);
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--line); border-radius: 14px;
  box-shadow: 0 10px 30px color-mix(in srgb, var(--ink) 25%, transparent);
}
.menu[hidden] { display: none; }
.menu button {
  display: block; width: 100%; min-height: 46px;
  padding: 0 14px; text-align: left;
  border: 0; border-radius: 10px;
  background: transparent; color: var(--text);
  font: inherit; cursor: pointer;
}
.menu button:active { background: var(--line); }
.menu button[data-action="new"] { color: var(--danger); font-weight: 600; }

/* Layout */
main {
  max-width: 640px; margin: 0 auto; padding: 8px;
  display: grid; gap: 8px;
  grid-template-columns: minmax(0, 1fr);
  overflow-x: clip;
}
main > * { min-width: 0; max-width: 100%; }
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
}
.grid-2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 8px; }
.grid-2 .card { min-width: 0; }

/* Bússola (adapta-se à largura e à altura da tela) */
.compass-card { text-align: center; padding-bottom: 10px; overflow: hidden; }
.compass {
  position: relative;
  width: min(56vw, 28dvh, 250px);
  aspect-ratio: 1;
  margin: 8px auto 2px;
}
.dial { width: 100%; height: 100%; will-change: transform; display: block; }
.dial-ring { fill: var(--accent); }
.dial-face { fill: var(--surface); stroke: var(--line); stroke-width: 1; }
.tick { stroke: var(--muted); stroke-width: 1; }
.tick.major { stroke: var(--text); stroke-width: 2; }
.dial text { fill: var(--text); font: 700 13px system-ui, sans-serif; text-anchor: middle; dominant-baseline: central; }
.dial text.card-n { fill: var(--red); font-size: 20px; }
.dial text.card-m { font-size: 18px; }
.dial text.deg { font-size: 9px; fill: var(--muted); font-weight: 600; }
.needle-n { fill: var(--red); }
.needle-s { fill: var(--gray); stroke: var(--muted); stroke-width: .5; }
.needle-pin { fill: var(--white); stroke: var(--accent); stroke-width: 3; }

.lubber {
  position: absolute; top: -7px; left: 50%; z-index: 2;
  width: 0; height: 0; transform: translateX(-50%);
  border-left: 10px solid transparent; border-right: 10px solid transparent;
  border-top: 18px solid var(--danger);
}
.readout-center {
  position: absolute; inset: 0; z-index: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  pointer-events: none; padding-top: 38%;
}
.hdg-value { font-size: 1.5rem; font-weight: 800; font-variant-numeric: tabular-nums; background: var(--surface); padding: 0 8px; border-radius: 8px; line-height: 1.2; }
.hdg-card { font-size: .85rem; font-weight: 700; color: var(--muted); background: var(--surface); padding: 0 6px; border-radius: 6px; }

.actions {
  display: grid;
  grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr) minmax(0, 1fr);
  gap: 8px; margin-top: 6px;
}

/* Inclinação */
.tilt { display: flex; align-items: center; gap: 10px; }
.level {
  position: relative; flex: none;
  width: 64px; height: 64px; border-radius: 50%;
  border: 3px solid var(--accent);
  background: var(--bg);
}
.level-h, .level-v { position: absolute; background: var(--line); }
.level-h { left: 4px; right: 4px; top: 50%; height: 1px; }
.level-v { top: 4px; bottom: 4px; left: 50%; width: 1px; }
.bubble {
  position: absolute; left: 50%; top: 50%;
  width: 18px; height: 18px; margin: -9px 0 0 -9px;
  border-radius: 50%; background: var(--accent);
  border: 2px solid var(--text);
  will-change: transform;
}
.bubble.flat { background: var(--red); }

.values { margin: 0; display: grid; gap: 6px; flex: 1; min-width: 0; }
.values > div { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; }
.values dt { color: var(--muted); font-size: .8rem; }
.values dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; font-size: .85rem; }
.tilt .values { gap: 4px; }
.tilt .values > div { flex-direction: column; align-items: flex-start; gap: 0; }
.tilt .values dd { font-size: 1.05rem; text-align: left; }
.values.compact { margin-top: 8px; gap: 4px; }

.meter { height: 10px; border-radius: 999px; background: var(--line); overflow: hidden; }
.meter span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .25s ease; }
.meter.low span { background: var(--red); }

.warning {
  margin: 0; padding: 8px 10px;
  border: 2px solid var(--danger); border-radius: 12px;
  font-size: .8rem; font-weight: 600;
}
.warning[hidden] { display: none; }

/* Declinação */
.field-label { display: block; font-weight: 600; font-size: .85rem; margin-bottom: 6px; }
.decl-row { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; width: 100%; }
.decl-row input {
  flex: 1 1 0; width: 0; min-width: 0; min-height: 48px; padding: 0 12px;
  border: 2px solid var(--line); border-radius: 12px;
  background: var(--bg); color: var(--text); font: inherit; font-size: 1.1rem; font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.decl-row .unit { font-weight: 700; font-size: 1.1rem; flex: none; }

/* Aba "Sobre Shen Kua e a declinação" com indicador */
details summary {
  min-height: 44px;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 0 14px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
  font-weight: 600; font-size: .9rem;
  list-style: none;
}
details summary::-webkit-details-marker { display: none; }
details summary::after {
  content: "";
  flex: none;
  width: 9px; height: 9px;
  margin-top: -4px;
  border-right: 3px solid var(--text);
  border-bottom: 3px solid var(--text);
  transform: rotate(45deg);
  transition: transform .2s ease;
}
details[open] summary { border-bottom-left-radius: 0; border-bottom-right-radius: 0; }
details[open] summary::after { transform: rotate(-135deg); margin-top: 4px; }
details > p:first-of-type { margin-top: 10px; }
details p { font-size: .9rem; }

.row-between { display: flex; align-items: center; justify-content: space-between; }
.badge { background: var(--accent); color: var(--ink); border-radius: 999px; padding: 0 10px; font-size: .85rem; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .85rem; font-variant-numeric: tabular-nums; }
th, td { padding: 8px 6px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; }
table[hidden] { display: none; }

.app-footer { text-align: center; color: var(--muted); padding: 4px 8px; }

/* Diálogos */
dialog {
  width: min(92vw, 460px);
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  padding: 20px;
}
dialog::backdrop { background: color-mix(in srgb, var(--ink) 60%, transparent); }
dialog ol { padding-left: 20px; margin: 0 0 12px; }
dialog li { margin-bottom: 6px; }
dialog a { color: var(--text); font-weight: 700; }
dialog form > .btn { width: 100%; margin-top: 8px; }
.figure8 { width: 100%; max-height: 140px; }
.f8-path { stroke: var(--accent); stroke-width: 6; stroke-linecap: round; }
.f8-dot { fill: var(--red); }
progress { width: 100%; height: 14px; accent-color: var(--gold); }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + var(--safe-b)); z-index: 60;
  transform: translateX(-50%);
  max-width: 90vw; padding: 12px 18px;
  background: var(--text); color: var(--bg);
  border-radius: 14px; font-weight: 600;
}
.toast[hidden] { display: none; }

/* Telas muito baixas */
@media (max-height: 640px) {
  .compass { width: min(50vw, 26dvh, 200px); }
  .app-header { padding-top: calc(3px + var(--safe-t)); padding-bottom: 3px; }
}

/* Impressão */
#print-report { display: none; }

@media print {
  @page { margin: 16mm; }
  body { background: #FFFFFF; color: #000; padding: 0; }
  body > *:not(#print-report) { display: none !important; }
  #print-report { display: block !important; color: #000; }
  #print-report h1 { font-size: 20pt; margin-bottom: 4pt; }
  #print-report p { margin: 0 0 4pt; font-size: 10pt; }
  #print-report table { margin-top: 12pt; font-size: 9pt; }
  #print-report th, #print-report td { border: 1px solid #D9D9D9; padding: 4pt 6pt; white-space: normal; }
  #print-report th { background: #F2F2F2; color: #000; }
  #print-report tr { break-inside: avoid; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
