:root {
  --bg: #eef2f5;
  --surface: #ffffff;
  --surface-2: #f5f7f9;
  --text: #1c2430;
  --muted: #5d6877;
  --border: #dbe1e7;
  --accent: #1f6fb2;
  --accent-soft: #e3eef8;
  --ok: #2b8a3e;
  --warn: #c2410c;
  --ad-bg: repeating-linear-gradient(45deg, #e9edf1, #e9edf1 10px, #e2e7ec 10px, #e2e7ec 20px);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161c;
    --surface: #19202a;
    --surface-2: #202834;
    --text: #e6ebf1;
    --muted: #97a3b2;
    --border: #2b3542;
    --accent: #5aa7ea;
    --accent-soft: #1d3045;
    --ok: #69db7c;
    --warn: #ff8a50;
    --ad-bg: repeating-linear-gradient(45deg, #1b222c, #1b222c 10px, #212935 10px, #212935 20px);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
.wrap { max-width: 1400px; margin: 0 auto; padding: 0 16px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.site-header { background: var(--surface); border-bottom: 1px solid var(--border); }
.site-header .wrap { display: flex; align-items: center; gap: 10px; height: 56px; }
.logo { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 8px; background: var(--accent); color: #fff; }
.brand { font-weight: 700; }
.badge { font-size: .72rem; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--accent); background: var(--accent-soft); border-radius: 99px; padding: 1px 8px; }
.lang { margin-left: auto; display: flex; align-items: center; gap: 6px; color: var(--muted); }
.lang select, .fld select {
  font: inherit; font-size: .9rem; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px;
}

.app { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 20px; align-items: start; margin: 16px auto 20px; }
@media (max-width: 1000px) { .app { grid-template-columns: minmax(0, 1fr); } }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }
.side { display: flex; flex-direction: column; gap: 16px; }
h2 { font-size: 1.1rem; margin: 0 0 10px; }
.lead { margin: 0 0 12px; color: var(--muted); }
.hint { margin: 6px 0 0; color: var(--muted); font-size: .84rem; }

/* Schede */
.tabs { display: flex; gap: 4px; margin-top: 14px; border-bottom: 1px solid var(--border); overflow-x: auto; }
.tab {
  font: inherit; font-weight: 600; font-size: .95rem; color: var(--muted); background: none; cursor: pointer; white-space: nowrap;
  border: 1px solid transparent; border-bottom: 0; border-radius: 10px 10px 0 0; padding: 9px 16px; margin-bottom: -1px;
}
.tab:hover { color: var(--text); }
.tab[aria-selected="true"] { color: var(--accent); background: var(--surface); border-color: var(--border); }
.tabpanel[hidden] { display: none; }

/* COLREG */
.cr-h1 { font-size: 1.25rem; margin: 0 0 4px; }
.cr-stage { background: #000; }
#crView { display: block; width: 100%; height: 100%; cursor: grab; touch-action: none; outline: none; }
#crView:active { cursor: grabbing; }
.cr-msg { position: absolute; inset: 0; display: grid; place-items: center; color: #fff; background: rgba(0,0,0,.6); padding: 20px; text-align: center; }
.cr-msg[hidden] { display: none; }
.cr-see { position: absolute; left: 10px; top: 10px; background: rgba(8, 22, 34, .72); color: #eaf3fa; border-radius: 8px; padding: 6px 10px; font-size: .84rem; pointer-events: none; max-width: 70%; }
.seen { margin-left: 8px; white-space: nowrap; }
.lamp { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 5px; }
.lamp-w { background: #fff6dc; } .lamp-r { background: #ff3b30; } .lamp-g { background: #2bd96b; } .lamp-y { background: #ffc233; }
.cr-info { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); gap: 18px; margin-top: 14px; }
@media (max-width: 760px) { .cr-info { grid-template-columns: minmax(0, 1fr); } }
.cr-table-wrap { overflow-x: auto; }
.cr-table { border-collapse: collapse; width: 100%; font-size: .84rem; font-variant-numeric: tabular-nums; }
.cr-table th { text-align: left; color: var(--muted); font-size: .74rem; font-weight: 650; padding: 3px 6px; }
.cr-table td { border-top: 1px solid var(--border); padding: 3px 6px; white-space: nowrap; color: var(--muted); }
.cr-table tr.on td { color: var(--text); font-weight: 600; }
.cr-h2b { margin-top: 14px; }
.cr-rules { font-size: .88rem; }
.cr-rules p { margin: 0 0 8px; }
#crSailRow[hidden], #crTowRow[hidden], #crHeelRow[hidden] { display: none; }

/* Scena */
.stage { position: relative; border-radius: 10px; overflow: hidden; background: #2d6a8a; aspect-ratio: 16 / 10; min-height: 320px; }
#view { display: block; width: 100%; height: 100%; outline: none; cursor: grab; touch-action: none; }
#view:active { cursor: grabbing; }
.mini { position: absolute; right: 10px; top: 10px; width: 200px; height: 140px; border-radius: 8px; border: 1px solid rgba(255,255,255,.5); background: rgba(10, 40, 60, .55); pointer-events: none; }
.hud {
  position: absolute; left: 10px; top: 10px; display: grid; grid-template-columns: auto auto; gap: 1px 12px;
  background: rgba(8, 22, 34, .72); color: #eaf3fa; border-radius: 8px; padding: 8px 12px; font-size: .82rem;
  font-variant-numeric: tabular-nums; pointer-events: none; min-width: 170px;
}
.hud b { font-weight: 650; }
.hud .k { color: #9fb7c9; }
.hud .bar { display: block; position: relative; height: 8px; border-radius: 4px; background: rgba(255,255,255,.18); margin-top: 5px; width: 90px; }
.hud .bar i { position: absolute; top: 0; bottom: 0; background: #ffb347; border-radius: 4px; }
.hud .bar::after { content: ""; position: absolute; left: 50%; top: -2px; bottom: -2px; width: 1px; background: rgba(255,255,255,.6); }
.wind-rose { position: absolute; left: 10px; bottom: 10px; width: 92px; height: 92px; pointer-events: none; }
.events { position: absolute; right: 10px; bottom: 10px; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; pointer-events: none; max-width: 60%; }
.events div { background: rgba(8, 22, 34, .72); color: #eaf3fa; border-radius: 6px; padding: 3px 8px; font-size: .8rem; }
.events .bad { background: rgba(160, 40, 20, .85); }
.banner {
  position: absolute; left: 50%; top: 56px; transform: translateX(-50%); background: rgba(8, 22, 34, .82); color: #ffd27a;
  border-radius: 8px; padding: 5px 14px; font-weight: 650; font-size: .88rem; pointer-events: none;
}
.banner[hidden] { display: none; }
.pause-icon { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); pointer-events: none; opacity: .9; }
.pause-icon[hidden] { display: none; }
.hud .bad { color: #ff8a6a; }
.overlay {
  position: absolute; inset: 0; display: grid; place-items: center; background: rgba(5, 18, 28, .55); color: #fff; text-align: center; padding: 20px;
}
.overlay[hidden] { display: none; }
.overlay .box { background: rgba(10, 30, 45, .92); border-radius: 12px; padding: 20px 26px; max-width: 460px; }
.overlay h3 { margin: 0 0 8px; font-size: 1.3rem; }
.overlay ol { text-align: left; margin: 8px 0 0; padding-left: 22px; font-size: .9rem; }

/* Barra a schermo */
.toolbar { position: absolute; top: 10px; right: 220px; display: flex; gap: 6px; z-index: 6; }
.tb {
  width: 38px; height: 38px; border-radius: 9px; border: 1px solid rgba(255,255,255,.35); cursor: pointer; font-size: 1rem; line-height: 1;
  background: rgba(8, 22, 34, .72); color: #fff; touch-action: manipulation;
}
.tb:hover { background: rgba(30, 70, 100, .9); }
.tb[hidden], .t-overlay [hidden] { display: none !important; }
.tb-menu { display: none; }
body.touch .tb-menu, body.fs .tb-menu { display: block; }
.menu-close { display: none; float: right; }

/* Comandi touch in sovraimpressione */
.t-overlay { display: none; position: absolute; inset: 0; pointer-events: none; z-index: 5; }
body.touch .t-overlay, body.fs .t-overlay { display: block; }
.t-overlay button { pointer-events: auto; touch-action: none; user-select: none; -webkit-user-select: none; }
.t-left, .t-right { position: absolute; bottom: 14px; display: flex; flex-direction: column; gap: 8px; }
.t-left { left: 12px; }
.t-right { right: 12px; align-items: flex-end; }
.t-row { display: flex; gap: 8px; }
.t-overlay .pad-btn {
  width: 58px; height: 52px; padding: 0; font-size: 1.25rem; border-radius: 12px;
  background: rgba(8, 22, 34, .55); color: #fff; border: 1px solid rgba(255,255,255,.4);
}
.t-overlay .pad-btn:active, .t-overlay .pad-btn.down { background: rgba(70, 150, 220, .85); }
.t-lines { position: absolute; bottom: 30px; left: 50%; transform: translateX(-50%); display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
.t-overlay .line-btn {
  min-width: 62px; padding: 6px 7px; font-size: .72rem; text-align: center; border-radius: 8px;
  background: rgba(8, 22, 34, .6); color: #fff; border: 1px solid rgba(255,255,255,.35);
}
.t-overlay .line-btn small, body.touch .t-overlay .line-btn kbd { display: none; }
.t-overlay .line-btn kbd { float: none; margin-right: 4px; background: transparent; color: #cfe0ee; border-color: rgba(255,255,255,.3); }
.t-overlay .line-btn.on { border-color: #4be07e; color: #9dffbc; }
.t-overlay .line-btn.reach { border-color: #6db8ff; background: rgba(40, 110, 180, .75); }
body:not(.touch) .t-left, body:not(.touch) .t-right { display: none; }
body.touch .controls { display: none; }
body.touch .banner { display: none; }
body.touch .mini { width: 110px; height: 77px; }
body.touch .toolbar { right: 128px; }
body.touch .hud { font-size: .68rem; padding: 5px 8px; min-width: 0; gap: 0 8px; }
body.touch .hud .bar { width: 60px; margin-top: 2px; height: 6px; }
body.touch .wind-rose { left: auto; right: 10px; top: 92px; bottom: auto; width: 58px; height: 58px; }
body.touch .wind-rose svg { width: 58px; height: 58px; }
@media (max-width: 480px) { body.touch .toolbar { top: auto; bottom: 200px; right: 12px; flex-direction: column; } body.touch .t-lines { bottom: 74px; } }

/* Schermo intero (funziona anche dove il browser non ha l'API, come su iPhone) */
body.fs { overflow: hidden; }
body.fs .site-header, body.fs .ad-slot, body.fs .tabs, body.fs .lead, body.fs .guide, body.fs .site-footer, body.fs .controls { display: none; }
body.fs .app { display: block; margin: 0; padding: 0; max-width: none; }
body.fs .sim { padding: 0; border: 0; border-radius: 0; background: #000; }
body.fs .stage { position: fixed; inset: 0; width: 100vw; height: 100vh; height: 100dvh; aspect-ratio: auto; border-radius: 0; z-index: 20; }
body.fs .side > :not(.setup), body.fs .setup { display: none; }
body.menu-open .setup {
  display: block; position: fixed; top: 0; right: 0; bottom: 0; width: min(380px, 94vw); overflow-y: auto; z-index: 60;
  border-radius: 0; box-shadow: -6px 0 24px rgba(0,0,0,.45);
}
body.menu-open .menu-close { display: block; }

/* Comandi sotto la scena */
.controls { display: flex; flex-wrap: wrap; gap: 12px 18px; align-items: center; margin-top: 12px; }
.lines { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.line-btn {
  font: inherit; font-size: .82rem; font-weight: 600; min-width: 112px; padding: 6px 10px; border-radius: 8px; cursor: pointer; line-height: 1.25;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text); text-align: left;
}
.line-btn small { display: block; font-weight: 400; color: var(--muted); font-size: .74rem; }
.line-btn kbd { float: right; }
.line-btn.on { border-color: var(--ok); }
.line-btn.on small { color: var(--ok); }
.line-btn.reach { border-color: var(--accent); background: var(--accent-soft); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 3px var(--accent-soft); } }
.pad { display: grid; grid-template-columns: repeat(3, auto); grid-template-areas: ". ahead ." "port neutral stbd" ". astern ." "thrP . thrS"; gap: 6px; }
.pad-btn {
  font: inherit; font-size: .82rem; font-weight: 600; padding: 7px 10px; border-radius: 8px; cursor: pointer; user-select: none; touch-action: none;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
}
.pad-btn:active, .pad-btn.down { background: var(--accent); color: #fff; border-color: var(--accent); }
.pad [data-tap="ahead"] { grid-area: ahead; }
.pad [data-tap="astern"] { grid-area: astern; }
.pad [data-hold="port"] { grid-area: port; }
.pad [data-hold="stbd"] { grid-area: stbd; }
.pad [data-tap="neutral"] { grid-area: neutral; }
.pad [data-hold="thrP"] { grid-area: thrP; }
.pad [data-hold="thrS"] { grid-area: thrS; }
.pad .thr[hidden] { display: none; }
.view-btns { display: flex; gap: 6px; margin-left: auto; }

kbd { font: 600 .74rem ui-monospace, Consolas, monospace; border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 4px; padding: 0 4px; background: var(--surface); color: var(--text); }
.keys { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; margin: 0; font-size: .86rem; }
.keys dt { white-space: nowrap; }
.keys dd { margin: 0; color: var(--muted); }

/* Impostazioni */
.fld { display: block; font-size: .86rem; font-weight: 550; margin-top: 10px; }
.fld select, .fld input[type=range] { display: block; width: 100%; margin-top: 4px; }
.fld output { float: right; font-weight: 600; color: var(--accent); font-variant-numeric: tabular-nums; }
input[type=range], input[type=checkbox], input[type=radio] { accent-color: var(--accent); }
.grp { border: 1px solid var(--border); border-radius: 10px; padding: 4px 12px 12px; margin: 14px 0 0; }
.grp legend { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 650; padding: 0 4px; }
.seg { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 6px; font-weight: 400; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 10px; }
.check { display: flex; gap: 6px; align-items: center; font-size: .86rem; }
.pick { font-weight: 650; color: var(--accent); min-width: 6em; }
.btn {
  font: inherit; font-weight: 600; font-size: .9rem; cursor: pointer; white-space: nowrap;
  background: var(--accent); color: #fff; border: 1px solid var(--accent); border-radius: 8px; padding: 8px 14px;
}
.btn.small { padding: 5px 10px; font-size: .84rem; }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.ghost:hover { border-color: var(--accent); color: var(--accent); }
.btn.go { width: 100%; margin-top: 16px; font-size: 1rem; padding: 10px; }

.guide { margin-bottom: 40px; }
.guide p { margin: 0 0 10px; }

.ad-slot { display: grid; place-items: center; text-align: center; background: var(--ad-bg); color: var(--muted); border: 1px dashed var(--border); border-radius: 8px; font-size: .8rem; letter-spacing: .03em; }
.ad-slot:empty { display: none; }
.ad-banner { min-height: 90px; margin-top: 16px; }
.ad-box { min-height: 250px; }
.ad-inline { min-height: 120px; margin: 18px 0 0; }
.ad-slot .ad-label { text-transform: uppercase; font-weight: 600; }

.site-footer { border-top: 1px solid var(--border); color: var(--muted); font-size: .85rem; }
.site-footer .wrap { padding-top: 16px; padding-bottom: 24px; }

@media (max-width: 600px) {
  .card { padding: 12px; }
  .stage { aspect-ratio: 3 / 4; }
  .mini { width: 120px; height: 84px; }
  .hud { font-size: .74rem; min-width: 0; }
  .view-btns { flex-direction: row; margin-left: 0; }
}
