:root {
  color-scheme: dark;
  --bg: #070b15;
  --surface: #101728;
  --surface-2: #151e33;
  --surface-3: #1b2740;
  --text: #f4f7ff;
  --muted: #9ca9c1;
  --line: #2a3650;
  --accent: #73e6b1;
  --accent-strong: #2fcf8a;
  --warning: #f6c85f;
  --danger: #ff7272;
  --info: #79b8ff;
  --red: #b9364c;
  --black: #171d2b;
  --green: #167c58;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html { background: var(--bg); }

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at top right, rgba(47, 207, 138, 0.12), transparent 32rem),
    radial-gradient(circle at top left, rgba(121, 184, 255, 0.10), transparent 26rem),
    var(--bg);
  color: var(--text);
}

button, input, textarea, select { font: inherit; }
button { touch-action: manipulation; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 1rem clamp(1rem, 4vw, 2rem);
  background: rgba(7, 11, 21, 0.88);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(16px);
}

h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0.15rem; font-size: clamp(1.45rem, 4vw, 2.15rem); }
h2 { margin-bottom: 0.35rem; }
h3 { margin-bottom: 0.4rem; }
.subtitle { margin: 0; color: var(--muted); font-size: 0.92rem; }
.eyebrow { margin: 0 0 0.25rem; color: var(--accent); font-size: 0.72rem; font-weight: 800; letter-spacing: 0.12em; text-transform: uppercase; }
.label { display: block; margin: 0 0 0.35rem; color: var(--muted); font-size: 0.78rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; }

.header-actions, .button-row, .play-tools, .action-topline, .history-heading, .log-heading {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  flex-wrap: wrap;
}

.page-shell {
  width: min(1180px, calc(100% - 2rem));
  margin: 1.2rem auto 3rem;
}

.action-panel, .panel, .metric-card {
  border: 1px solid var(--line);
  background: linear-gradient(145deg, rgba(21, 30, 51, 0.98), rgba(12, 18, 32, 0.98));
  box-shadow: var(--shadow);
}

.action-panel {
  padding: clamp(1rem, 3vw, 1.6rem);
  border-radius: 1.35rem;
  overflow: hidden;
}

.action-main {
  display: grid;
  grid-template-columns: minmax(105px, 150px) 1fr;
  gap: 1.25rem;
  align-items: center;
  margin-top: 0.9rem;
}

.target-number {
  display: grid;
  place-items: center;
  width: clamp(90px, 20vw, 130px);
  aspect-ratio: 1;
  border-radius: 50%;
  background: radial-gradient(circle at 30% 25%, #334664, #111827 58%, #070b15 100%);
  border: 3px solid var(--accent);
  font-size: clamp(2.5rem, 10vw, 4.5rem);
  font-weight: 900;
  line-height: 1;
  box-shadow: 0 0 0 7px rgba(115, 230, 177, 0.08);
}

.action-copy h2 { font-size: clamp(1.2rem, 3vw, 1.85rem); }
.action-copy p { color: var(--muted); margin-bottom: 0.75rem; }

.phase-badge, .trigger-badge, .status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 850;
  letter-spacing: 0.06em;
}
.phase-badge { background: rgba(121, 184, 255, 0.14); color: #b9dbff; border: 1px solid rgba(121, 184, 255, 0.35); }
.phase-badge.attack { background: rgba(115, 230, 177, 0.14); color: var(--accent); border-color: rgba(115, 230, 177, 0.38); }
.phase-badge.limit { background: rgba(255, 114, 114, 0.14); color: #ffadad; border-color: rgba(255, 114, 114, 0.38); }
.trigger-badge { background: rgba(246, 200, 95, 0.14); color: var(--warning); border: 1px solid rgba(246, 200, 95, 0.35); }
.status-pill { background: rgba(115, 230, 177, 0.10); color: var(--accent); }
.status-pill.offline { background: rgba(246, 200, 95, 0.10); color: var(--warning); }

.cinquina-wrap { margin-top: 0.7rem; }
.cinquina, .history-chips { display: flex; gap: 0.45rem; flex-wrap: wrap; }
.number-chip {
  display: inline-grid;
  place-items: center;
  min-width: 34px;
  height: 34px;
  padding: 0 0.45rem;
  border-radius: 999px;
  background: var(--surface-3);
  border: 1px solid var(--line);
  font-weight: 800;
}
.number-chip.leader { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(115, 230, 177, 0.09); }

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 0.8rem;
  margin: 0.9rem 0;
}
.metric-card {
  min-height: 112px;
  padding: 0.9rem;
  border-radius: 1rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.metric-card.emphasis { border-color: rgba(115, 230, 177, 0.55); }
.metric-card.debt { border-color: rgba(246, 200, 95, 0.35); }
.metric-label { color: var(--muted); font-size: 0.76rem; font-weight: 700; }
.metric-card strong { margin: 0.2rem 0 0.2rem; font-size: clamp(1.2rem, 2.4vw, 1.65rem); }
.metric-card small { color: var(--muted); line-height: 1.25; }
.positive { color: var(--accent) !important; }
.negative { color: var(--danger) !important; }

.panel {
  margin-top: 0.9rem;
  border-radius: 1.15rem;
  overflow: hidden;
}
.panel-heading, .details-body { padding: 1rem; }
.panel-heading { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }
.panel-heading h2 { margin: 0; }

summary {
  padding: 1rem;
  cursor: pointer;
  font-weight: 850;
  list-style: none;
  user-select: none;
}
summary::-webkit-details-marker { display: none; }
summary::after { content: '＋'; float: right; color: var(--accent); }
details[open] > summary::after { content: '−'; }
details[open] > summary { border-bottom: 1px solid var(--line); }

.hint { color: var(--muted); font-size: 0.88rem; line-height: 1.45; }

.number-grid {
  display: grid;
  grid-template-columns: repeat(10, minmax(42px, 1fr));
  gap: 0.5rem;
  padding: 0 1rem 1rem;
}
.number-button {
  min-height: 52px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 0.75rem;
  color: white;
  font-weight: 900;
  font-size: 1rem;
  cursor: pointer;
  transition: transform 120ms ease, filter 120ms ease, border-color 120ms ease;
}
.number-button:hover { filter: brightness(1.15); border-color: white; }
.number-button:active { transform: scale(0.95); }
.number-button.red { background: linear-gradient(145deg, #c54058, #812638); }
.number-button.black { background: linear-gradient(145deg, #30394c, #101621); }
.number-button.green { background: linear-gradient(145deg, #1d9a70, #0b533b); }

.history-block { padding: 0 1rem 1rem; }
.history-heading { justify-content: space-between; }
.history-heading h3 { margin: 0; }
.history-heading span { color: var(--muted); font-size: 0.8rem; }
.history-chips { margin-top: 0.75rem; }
.history-chips .number-chip { min-width: 31px; height: 31px; font-size: 0.84rem; }

.button {
  min-height: 40px;
  padding: 0.62rem 0.86rem;
  border-radius: 0.72rem;
  border: 1px solid transparent;
  font-weight: 800;
  cursor: pointer;
}
.button:disabled { opacity: 0.45; cursor: not-allowed; }
.button.primary { color: #04140d; background: var(--accent); }
.button.primary:hover { background: var(--accent-strong); }
.button.secondary { color: var(--text); background: var(--surface-3); border-color: var(--line); }
.button.ghost { color: var(--muted); background: transparent; border-color: var(--line); }
.button.danger { color: #ffdede; background: rgba(255, 114, 114, 0.13); border-color: rgba(255, 114, 114, 0.35); }
.button-row.compact .button { min-height: 34px; padding: 0.45rem 0.65rem; font-size: 0.85rem; }

.switch-label, .checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  color: var(--muted);
  font-size: 0.9rem;
}
.switch-label input:checked + span { color: var(--warning); font-weight: 800; }

.import-layout { display: grid; grid-template-columns: 1.45fr 1fr; gap: 1rem; }
.import-form, .photo-helper { min-width: 0; }
.form-row, .settings-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.8rem;
  margin: 0.8rem 0;
}
.settings-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.full-width { grid-column: 1 / -1; }

label { display: grid; gap: 0.38rem; color: var(--muted); font-size: 0.85rem; font-weight: 700; }
input, textarea, select {
  width: 100%;
  color: var(--text);
  background: #090f1d;
  border: 1px solid var(--line);
  border-radius: 0.72rem;
  padding: 0.72rem 0.78rem;
  outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(115, 230, 177, 0.10); }
textarea { resize: vertical; line-height: 1.5; }

.photo-preview-wrap { margin-top: 0.75rem; }
.photo-preview-wrap img {
  display: block;
  width: 100%;
  max-height: 340px;
  object-fit: contain;
  background: #050810;
  border: 1px solid var(--line);
  border-radius: 0.8rem;
  transform-origin: center;
  margin-bottom: 0.65rem;
}

.diagnostic-summary {
  padding: 0.8rem;
  border-radius: 0.8rem;
  background: rgba(121, 184, 255, 0.08);
  border: 1px solid rgba(121, 184, 255, 0.20);
  color: #cfe7ff;
  margin-bottom: 0.8rem;
  line-height: 1.45;
}
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 680px; }
th, td { padding: 0.62rem; border-bottom: 1px solid var(--line); text-align: right; font-size: 0.85rem; }
th { color: var(--muted); font-size: 0.74rem; text-transform: uppercase; }
th:nth-child(2), td:nth-child(2), th:last-child, td:last-child { text-align: left; }
tr.top-five { background: rgba(115, 230, 177, 0.06); }
tr.leader-row { background: rgba(115, 230, 177, 0.13); }

.file-button { display: inline-flex; width: auto; }
.log-heading { justify-content: space-between; margin-top: 1rem; }
.log-heading h3 { margin: 0; }
.event-log { display: grid; gap: 0.55rem; margin-top: 0.7rem; max-height: 420px; overflow: auto; }
.log-entry { padding: 0.7rem; border-radius: 0.72rem; background: #0a1120; border: 1px solid var(--line); }
.log-entry strong { display: block; margin-bottom: 0.2rem; font-size: 0.8rem; color: var(--accent); }
.log-entry p { margin: 0; color: #dbe4f4; font-size: 0.86rem; line-height: 1.35; }
.log-entry time { display: block; color: var(--muted); font-size: 0.72rem; margin-top: 0.25rem; }
.empty { color: var(--muted); }

footer { width: min(1180px, calc(100% - 2rem)); margin: 0 auto 2rem; color: var(--muted); font-size: 0.8rem; text-align: center; }
.toast {
  position: fixed;
  left: 50%;
  bottom: max(1rem, env(safe-area-inset-bottom));
  z-index: 50;
  transform: translateX(-50%);
  width: min(92vw, 540px);
  padding: 0.85rem 1rem;
  border-radius: 0.85rem;
  background: #edf9f3;
  color: #092015;
  box-shadow: var(--shadow);
  font-weight: 750;
  text-align: center;
}

@media (max-width: 980px) {
  .metrics-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .number-grid { grid-template-columns: repeat(8, minmax(42px, 1fr)); }
  .settings-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 700px) {
  .app-header { align-items: flex-start; }
  .subtitle { display: none; }
  .page-shell { width: min(100% - 1rem, 1180px); margin-top: 0.6rem; }
  .action-main { grid-template-columns: 95px 1fr; gap: 0.9rem; }
  .target-number { width: 88px; font-size: 2.8rem; }
  .metrics-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.55rem; }
  .metric-card { min-height: 98px; padding: 0.72rem; }
  .number-grid { grid-template-columns: repeat(6, minmax(42px, 1fr)); gap: 0.42rem; }
  .number-button { min-height: 48px; }
  .import-layout, .form-row, .settings-grid { grid-template-columns: 1fr; }
  .panel-heading { align-items: flex-start; flex-direction: column; }
  .play-tools { width: 100%; justify-content: space-between; }
}

@media (max-width: 390px) {
  .number-grid { grid-template-columns: repeat(5, minmax(42px, 1fr)); }
  .action-main { grid-template-columns: 1fr; }
  .target-number { width: 82px; }
}
.last-outcome {
  display: inline-block;
  margin: 0 0 0.65rem !important;
  padding: 0.45rem 0.65rem;
  border-radius: 0.65rem;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--line);
  color: #dbe4f4 !important;
  font-size: 0.84rem;
}
.toast.error { background: #ffe7e7; color: #4c1111; }
.photo-preview-wrap img.rotate-0 { transform: rotate(0deg); }
.photo-preview-wrap img.rotate-90 { transform: rotate(90deg); }
.photo-preview-wrap img.rotate-180 { transform: rotate(180deg); }
.photo-preview-wrap img.rotate-270 { transform: rotate(270deg); }

/* C5 Echo Multi v2 */
:root {
  --rapid: #ff9d55;
  --rapid-soft: rgba(255, 157, 85, 0.12);
  --violet: #a996ff;
}

.metric-card.emphasis-alt { border-color: rgba(169, 150, 255, 0.48); }
.action-panel.rapid-active {
  border-color: rgba(255, 157, 85, 0.72);
  box-shadow: 0 18px 55px rgba(255, 106, 45, 0.16);
}
.risk-badge {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.05em;
  color: #ffd5ba;
  background: rgba(255, 119, 58, 0.16);
  border: 1px solid rgba(255, 157, 85, 0.5);
}
.strategy-badge {
  color: #dfe6f5;
  font-size: 0.8rem;
  font-weight: 800;
  padding: 0.35rem 0.65rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--line);
}
.target-block { display: grid; justify-items: center; gap: 0.45rem; }
.active-table-label {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
}
.mother-five {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.45rem;
}
.mother-five::before {
  content: 'Cinquina madre:';
  color: var(--muted);
  font-size: 0.8rem;
  align-self: center;
  margin-right: 0.2rem;
}
.reentry-banner {
  margin-top: 0.7rem;
  padding: 0.65rem 0.75rem;
  border-radius: 0.75rem;
  color: #fff0c8;
  background: rgba(246, 200, 95, 0.09);
  border: 1px solid rgba(246, 200, 95, 0.28);
  font-size: 0.85rem;
}
.scanner-controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(160px, 1fr));
  gap: 0.7rem;
  width: min(760px, 100%);
}
.compact-check { align-content: end; padding-bottom: 0.7rem; }
.rapid-warning {
  margin: 0 1rem 1rem;
  padding: 0.75rem 0.85rem;
  border-radius: 0.8rem;
  color: #ffe0c9;
  background: var(--rapid-soft);
  border: 1px solid rgba(255, 157, 85, 0.4);
}
.table-tabs {
  display: flex;
  gap: 0.55rem;
  flex-wrap: wrap;
  padding: 0 1rem 0.8rem;
}
.table-tab {
  min-width: 105px;
  min-height: 43px;
  padding: 0.55rem 0.8rem;
  border-radius: 0.8rem;
  border: 1px solid var(--line);
  color: var(--muted);
  background: #0b1221;
  cursor: pointer;
  font-weight: 850;
}
.table-tab.selected {
  color: var(--text);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(115, 230, 177, 0.08);
}
.table-tab.active-attack { color: var(--accent); }
.table-tab.rapid-candidate { border-color: rgba(255, 157, 85, 0.5); }
.table-cards {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.7rem;
  padding: 0 1rem 1rem;
}
.table-card {
  min-width: 0;
  padding: 0.85rem;
  border-radius: 0.9rem;
  border: 1px solid var(--line);
  background: rgba(7, 13, 24, 0.72);
}
.table-card.selected { border-color: rgba(115, 230, 177, 0.45); }
.table-card.active { background: rgba(115, 230, 177, 0.07); }
.table-card h3 { display: flex; justify-content: space-between; gap: 0.5rem; margin-bottom: 0.55rem; }
.table-card h3 span { color: var(--muted); font-size: 0.75rem; }
.table-status-line { display: flex; justify-content: space-between; gap: 0.5rem; margin: 0.32rem 0; font-size: 0.8rem; }
.table-status-line span:first-child { color: var(--muted); }
.table-status-value { font-weight: 850; text-align: right; }
.table-status-value.good { color: var(--accent); }
.table-status-value.rapid { color: var(--rapid); }
.shadow-line {
  margin-top: 0.55rem;
  padding-top: 0.55rem;
  border-top: 1px dashed var(--line);
  color: #ffc7a2;
  font-size: 0.78rem;
}
.strategy-description {
  padding: 0.8rem;
  border-radius: 0.8rem;
  background: rgba(169, 150, 255, 0.08);
  border: 1px solid rgba(169, 150, 255, 0.22);
  color: #ddd7ff;
  line-height: 1.45;
}
.progression-table-wrap { max-height: 520px; overflow: auto; }
.progression-table-wrap table { min-width: 780px; }
.progression-table-wrap tr.current-row { background: rgba(115, 230, 177, 0.14); }
.progression-table-wrap tr.reentry-row { background: rgba(246, 200, 95, 0.12); }
.rapid-stats-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.7rem;
  margin-bottom: 0.8rem;
}
.rapid-stat-card {
  padding: 0.8rem;
  border-radius: 0.85rem;
  background: rgba(255, 157, 85, 0.06);
  border: 1px solid rgba(255, 157, 85, 0.22);
}
.rapid-stat-card h3 { margin-bottom: 0.55rem; }
.rapid-stat-row { display: flex; justify-content: space-between; gap: 0.5rem; color: var(--muted); font-size: 0.82rem; margin-top: 0.3rem; }
.rapid-stat-row strong { color: var(--text); }
.diagnostic-columns {
  display: grid;
  grid-template-columns: 1.25fr 0.75fr;
  gap: 1rem;
}
.rapid-windows { display: grid; gap: 0.55rem; }
.rapid-window {
  padding: 0.65rem;
  border-radius: 0.7rem;
  border: 1px solid var(--line);
  background: #090f1d;
  font-size: 0.82rem;
}
.rapid-window strong { color: var(--rapid); }

@media (max-width: 980px) {
  .table-cards, .rapid-stats-grid { grid-template-columns: 1fr; }
  .scanner-controls { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .diagnostic-columns { grid-template-columns: 1fr; }
}

@media (max-width: 700px) {
  .scanner-controls { grid-template-columns: 1fr; width: 100%; }
  .compact-check { padding-bottom: 0; }
  .table-tabs { overflow-x: auto; flex-wrap: nowrap; }
  .table-tab { flex: 0 0 auto; }
}

/* C5 Echo Multi v2 */
.metrics-grid { grid-template-columns: repeat(auto-fit, minmax(145px, 1fr)); }
.five-layout { grid-template-columns: minmax(320px, 0.95fr) 1.35fr; }
.five-target-block { display: grid; align-content: center; gap: 0.5rem; }
.active-five {
  display: grid;
  grid-template-columns: repeat(5, minmax(50px, 1fr));
  gap: 0.45rem;
  width: min(100%, 500px);
}
.active-five .number-chip {
  width: 100%;
  min-width: 0;
  height: 68px;
  border-radius: 0.9rem;
  font-size: 1.45rem;
}
.empty-five {
  grid-column: 1 / -1;
  display: grid;
  place-items: center;
  min-height: 68px;
  color: var(--muted);
  border: 1px dashed var(--line);
  border-radius: 0.9rem;
}
.number-button.active-target {
  box-shadow: 0 0 0 3px rgba(255, 211, 106, 0.88), 0 0 22px rgba(255, 211, 106, 0.26);
  border-color: #fff0b1;
  filter: brightness(1.18);
}
.panel-foot-hint { padding: 0 1rem 1rem; margin: 0; }

@media (max-width: 700px) {
  .five-layout { grid-template-columns: 1fr; }
  .active-five { width: 100%; }
  .active-five .number-chip { height: 58px; font-size: 1.2rem; }
}

@media (max-width: 390px) {
  .active-five { grid-template-columns: repeat(5, minmax(42px, 1fr)); gap: 0.3rem; }
  .active-five .number-chip { height: 52px; padding: 0; }
}
.number-chip.red { background: linear-gradient(145deg, #b9364c, #7c2435); border-color: rgba(255,255,255,0.2); }
.number-chip.black { background: linear-gradient(145deg, #30394c, #101621); border-color: rgba(255,255,255,0.16); }
.number-chip.green { background: linear-gradient(145deg, #1d9a70, #0b533b); border-color: rgba(255,255,255,0.2); }
.shadow-five { color: #ffc7a2; font-weight: 850; font-size: 0.83rem; margin-bottom: 0.4rem; letter-spacing: 0.03em; }

@media (max-width: 980px) {
  .table-cards,   .scanner-controls { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  }

@media (max-width: 700px) {
  .scanner-controls { grid-template-columns: 1fr; width: 100%; }
  .compact-check { padding-bottom: 0; }
  .table-tabs { overflow-x: auto; flex-wrap: nowrap; }
  .table-tab { flex: 0 0 auto; }
}

.stable-note {
  margin: 0 0 1rem;
  padding: 0.9rem 1rem;
  border: 1px solid rgba(84, 195, 255, 0.28);
  border-radius: 14px;
  background: rgba(14, 42, 65, 0.72);
  color: var(--muted, #b7c9d9);
  line-height: 1.5;
}

.stable-note strong {
  color: var(--text, #f5fbff);
}

.resume-section .diagnostic-summary {
  min-height: 3.2rem;
  display: flex;
  align-items: center;
}

@media (max-width: 700px) {
  .stable-note {
    font-size: 0.92rem;
  }
}

.history-heading > div { min-width: 0; }
.history-heading small {
  display: block;
  margin-top: 0.2rem;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 500;
}
.history-chips { max-height: 360px; }
