:root {
    --bg: #000;
    --panel: #121212;
    --panel-2: #1a1a1a;
    --line: rgba(255, 255, 255, 0.08);
    --text: #f2f2f2;
    --muted: #8a8a8a;
    --accent: #00ff9d;
    --accent-2: #00e0ff;
    --warn: #fbbf24;
    --bad: #ff3b5c;
    --wood: #3d2817;
    --radius: 14px;
    --safe-t: env(safe-area-inset-top, 0px);
    --safe-b: env(safe-area-inset-bottom, 0px);
    --safe-l: env(safe-area-inset-left, 0px);
    --safe-r: env(safe-area-inset-right, 0px);
}

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

html, body { background: var(--bg); color: var(--text); }

body {
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", sans-serif;
    font-size: 15px;
    line-height: 1.45;
    -webkit-text-size-adjust: 100%;
    -webkit-user-select: none;
    user-select: none;
    overscroll-behavior-y: none;
}

input, select { -webkit-user-select: text; user-select: text; }

.app {
    max-width: 980px;
    margin: 0 auto;
    padding: calc(8px + var(--safe-t)) calc(14px + var(--safe-r)) calc(18px + var(--safe-b)) calc(14px + var(--safe-l));
}

/* ---------- barra in alto ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; min-height: 60px; margin-bottom: 4px; }
.brand { font-weight: 900; font-size: clamp(1.55em, 6.2vw, 2.2em); letter-spacing: .2px; line-height: 1.1; }
.brand span {
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    -webkit-background-clip: text; background-clip: text; color: transparent;
}
.topbar-actions { display: flex; gap: 8px; }

/* ---------- pulsanti ---------- */
button, .filebtn {
    font: inherit; font-weight: 600; color: var(--text);
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: 12px;
    min-height: 44px;                      /* misura minima di tocco Apple */
    padding: 10px 14px;
    cursor: pointer;
    touch-action: manipulation;
    transition: transform .12s ease, background .2s ease, border-color .2s ease, opacity .2s;
}
button:active:not(:disabled), .filebtn:active { transform: scale(0.97); }
button:disabled { opacity: .4; cursor: default; }

button.primary {
    background: linear-gradient(135deg, rgba(0,255,157,.22), rgba(0,224,255,.18));
    border-color: rgba(0,255,157,.55);
}
button.ghost { background: transparent; }
button.danger { border-color: rgba(255,59,92,.45); color: #ffb3c0; }
button.wide { width: 100%; }

.icon-btn { width: 44px; height: 44px; padding: 0; font-size: 1.25em; border-radius: 50%; }

@media (hover: hover) {
    button:hover:not(:disabled) { border-color: rgba(0,255,157,.5); }
}

/* ---------- tavolo ---------- */
.table-wrap { display: flex; justify-content: center; margin: 4px 0 12px; }

.table-frame {
    position: relative;
    border: 14px solid var(--wood);
    border-radius: 14px;
    background: #0a4a0a;
    box-shadow: 0 14px 40px rgba(0,0,0,.7), inset 0 0 40px rgba(0,0,0,.5);
    line-height: 0;
}
#poolTable { display: block; touch-action: none; cursor: crosshair; border-radius: 3px; }

.busy {
    position: absolute; inset: 0;
    display: none; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
    background: rgba(0,0,0,.45);
    color: #fff; font-weight: 600; line-height: 1.3;
    border-radius: 3px;
}
.busy.on { display: flex; }
.spinner {
    width: 34px; height: 34px; border-radius: 50%;
    border: 3px solid rgba(255,255,255,.2); border-top-color: var(--accent);
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- azioni ---------- */
.actions { display: grid; grid-template-columns: 1fr 1fr 52px; gap: 10px; }

.message {
    margin: 12px 0;
    padding: 12px 14px;
    border-left: 3px solid var(--accent);
    background: rgba(0,255,157,.05);
    border-radius: 10px;
    color: #d6d6d6;
    min-height: 48px;
}
.message strong { color: var(--accent); }
.message.bad { border-left-color: var(--bad); }
.message.bad strong { color: var(--bad); }
.message button { margin-top: 8px; min-height: 36px; padding: 6px 12px; }

/* ---------- schede ---------- */
.tabs {
    display: grid; grid-template-columns: repeat(3, 1fr);
    background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
    padding: 4px; gap: 4px; margin-bottom: 12px;
}
.tab { background: transparent; border: none; min-height: 38px; color: var(--muted); }
.tab.active { background: var(--panel-2); color: var(--text); box-shadow: 0 1px 0 rgba(255,255,255,.06) inset; }

.panel { display: none; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.panel.active { display: block; }
.panel h3 {
    font-size: .74em; text-transform: uppercase; letter-spacing: 1.4px;
    color: var(--accent); margin: 16px 0 10px;
}
.panel h3:first-child { margin-top: 0; }
.hint { color: var(--muted); font-size: .86em; margin: 10px 0 4px; }

/* ---------- analisi ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat {
    background: rgba(0,255,157,.04); border: 1px solid rgba(0,255,157,.1);
    border-radius: 10px; padding: 10px 6px; text-align: center;
}
.stat-label { font-size: .68em; color: var(--muted); text-transform: uppercase; letter-spacing: .8px; }
.stat-value { font-size: 1.25em; font-weight: 800; color: var(--accent); font-variant-numeric: tabular-nums; margin-top: 2px; }

.hitpoint { display: flex; align-items: center; gap: 16px; margin-top: 14px; }
#cueBallCanvas { width: 110px; height: 110px; flex: none; }
.hitpoint h4 { font-size: .8em; color: var(--accent); text-transform: uppercase; letter-spacing: 1.2px; margin-bottom: 8px; }
.hit-labels { display: flex; flex-wrap: wrap; gap: 6px; }
.hit-label { font-size: .78em; padding: 4px 8px; border-radius: 6px; background: rgba(255,255,255,.05); color: var(--muted); }
.hit-label.active { background: rgba(0,255,157,.15); color: var(--accent); font-weight: 700; }
.legend { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 10px; font-size: .78em; color: var(--muted); }
.legend i { display: inline-block; width: 16px; height: 3px; border-radius: 2px; margin-right: 5px; vertical-align: middle; }

/* ---------- palle ---------- */
.ball-selector { display: grid; grid-template-columns: repeat(8, 1fr); gap: 8px; }
.ball-btn {
    aspect-ratio: 1; min-height: 0; padding: 0; border-radius: 50%;
    font-size: 13px; font-weight: 800;
    border: 2px solid rgba(255,255,255,.15);
    box-shadow: inset 0 2px 6px rgba(255,255,255,.25), inset 0 -3px 6px rgba(0,0,0,.35);
}
.ball-btn.active { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(0,255,157,.35); }
.ball-btn.ontable { opacity: .45; }
.ball-btn.ontable.active { opacity: 1; }

.seg { display: flex; gap: 6px; background: var(--panel-2); padding: 4px; border-radius: 12px; border: 1px solid var(--line); }
.seg button { flex: 1; background: transparent; border: none; min-height: 38px; font-size: .9em; color: var(--muted); padding: 6px 8px; }
.seg button.on { background: rgba(0,255,157,.16); color: var(--text); }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid2 .danger { grid-column: 1 / -1; }

.foot { text-align: center; color: #555; font-size: .75em; margin-top: 16px; }

/* ---------- finestra scansione ---------- */
.sheet-backdrop {
    position: fixed; inset: 0; z-index: 50;
    background: rgba(0,0,0,.75);
    display: none; align-items: flex-end; justify-content: center;
}
.sheet-backdrop.open { display: flex; }
.sheet {
    width: min(760px, 100%);
    max-height: calc(100% - var(--safe-t) - 12px);
    overflow-y: auto; -webkit-overflow-scrolling: touch;
    background: #0e0e0e;
    border: 1px solid rgba(0,255,157,.25); border-bottom: none;
    border-radius: 20px 20px 0 0;
    padding: 16px 16px calc(20px + var(--safe-b));
}
@media (min-width: 800px) {
    .sheet-backdrop { align-items: center; }
    .sheet { border-radius: 20px; border-bottom: 1px solid rgba(0,255,157,.25); }
}
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.sheet h2 { font-size: 1.15em; color: var(--accent); }
.sheet fieldset { border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-bottom: 12px; }
.sheet legend { padding: 0 6px; font-size: .72em; font-weight: 700; color: var(--accent); text-transform: uppercase; letter-spacing: 1.3px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row > * { flex: 1 1 150px; }
.filebtn { display: inline-flex; align-items: center; justify-content: center; text-align: center;
           border-color: rgba(0,255,157,.45); }
.filebtn input { display: none; }
#camVideo, #photoCanvas { width: 100%; border-radius: 10px; margin-top: 10px; background: #000; display: none; }
#photoCanvas { touch-action: none; }
.sheet input[type=password], .sheet select {
    width: 100%; margin-top: 10px; padding: 12px;
    background: var(--panel-2); color: var(--text);
    border: 1px solid var(--line); border-radius: 10px; font: inherit; font-size: 16px; /* 16px evita lo zoom di iOS */
}
.check { display: flex; gap: 10px; align-items: center; color: #bbb; font-size: .86em; margin-top: 10px; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); flex: none; }
.scan-status { min-height: 22px; font-size: .88em; color: var(--warn); margin: 4px 0 12px; }
.scan-status.ok { color: var(--accent); }
.scan-status.bad { color: var(--bad); }

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