/* UNARMA AI — stile applicativo. Nessuno stile inline: compatibile con CSP "style-src 'self'". */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --sidebar: #0f1d33;
  --sidebar-text: #d9e1ee;
  --sidebar-muted: #8fa0ba;
  --sidebar-hover: #1b2d4a;
  --text: #17202e;
  --muted: #5d6a7e;
  --border: #dde2ea;
  --primary: #14325c;
  --primary-hover: #0d2647;
  --primary-contrast: #ffffff;
  --accent: #b5172b;
  --success: #1f7a4d;
  --warning: #9a6200;
  --danger: #b42318;
  --code-bg: #0f172a;
  --code-text: #e2e8f0;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15, 29, 51, .06), 0 4px 16px rgba(15, 29, 51, .06);
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d131d;
    --surface: #141c28;
    --surface-2: #1b2534;
    --sidebar: #090e16;
    --sidebar-hover: #162133;
    --text: #e6ebf2;
    --muted: #96a3b6;
    --border: #263245;
    --primary: #4f86d9;
    --primary-hover: #6797e0;
    --accent: #e0485a;
    --success: #3fb27f;
    --warning: #e0a63a;
    --danger: #f06b5f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; font-family: var(--font); font-size: 15px; line-height: 1.55; color: var(--text); background: var(--bg); }
a { color: var(--primary); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .6em; }
h1 { font-size: 1.5rem; } h2 { font-size: 1.2rem; } h3 { font-size: 1.05rem; }
small, .muted { color: var(--muted); }
code { font-family: var(--mono); font-size: .9em; }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Pulsanti e form */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .4em; padding: .55em 1em; border-radius: 9px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-weight: 500; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-contrast); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.btn-danger:hover { background: color-mix(in srgb, var(--danger) 10%, var(--surface)); }
.btn-sm { padding: .3em .7em; font-size: .85rem; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-block { width: 100%; }

label { display: block; font-weight: 500; margin-bottom: .3em; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=search], input[type=file], select, textarea {
  width: 100%; padding: .6em .75em; border: 1px solid var(--border); border-radius: 9px; background: var(--surface); color: var(--text); font: inherit;
}
textarea { resize: vertical; }
input[type=checkbox] { width: 1.05em; height: 1.05em; accent-color: var(--primary); }
.field { margin-bottom: 1rem; }
.field .helptext, .helptext { display: block; font-size: .82rem; color: var(--muted); margin-top: .25em; }
.field.checkbox { display: flex; align-items: center; gap: .5em; }
.field.checkbox label { margin: 0; order: 2; }
.errorlist { color: var(--danger); list-style: none; padding: 0; margin: .3em 0 0; font-size: .87rem; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 0 1.2rem; }

/* Messaggi flash */
.flash-stack { position: fixed; top: 1rem; right: 1rem; z-index: 50; display: flex; flex-direction: column; gap: .5rem; max-width: min(420px, calc(100vw - 2rem)); }
.flash { padding: .75em 1em; border-radius: 10px; background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow); border-left: 4px solid var(--primary); }
.flash-success { border-left-color: var(--success); }
.flash-error { border-left-color: var(--danger); }
.flash-warning { border-left-color: var(--warning); }
.alert { padding: .75em 1em; border-radius: 10px; border: 1px solid var(--border); margin-bottom: 1rem; background: var(--surface-2); }
.alert-error { border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); color: var(--danger); }
.alert-warning { border-color: color-mix(in srgb, var(--warning) 45%, var(--border)); }

/* Pagine di autenticazione */
.auth-page { min-height: 100%; display: grid; place-items: center; padding: 1.5rem; background: radial-gradient(circle at 20% 0%, color-mix(in srgb, var(--primary) 18%, var(--bg)), var(--bg) 55%); }
.auth-card { width: 100%; max-width: 420px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 2rem; box-shadow: var(--shadow); }
.auth-card.wide { max-width: 560px; }
.brand { display: flex; align-items: center; gap: .6rem; font-weight: 700; font-size: 1.15rem; margin-bottom: 1.5rem; }
.brand-mark { width: 36px; height: 36px; border-radius: 10px; background: linear-gradient(135deg, var(--primary), var(--accent)); color: #fff; display: grid; place-items: center; font-weight: 800; font-size: .95rem; letter-spacing: -.02em; }
.qr { display: block; width: 220px; height: 220px; margin: 1rem auto; background: #fff; padding: 8px; border-radius: 10px; }
.secret { font-family: var(--mono); background: var(--surface-2); padding: .5em .7em; border-radius: 8px; word-break: break-all; text-align: center; }
.codes { display: grid; grid-template-columns: repeat(2, 1fr); gap: .5rem; font-family: var(--mono); font-size: 1.05rem; padding: 0; list-style: none; }
.codes li { background: var(--surface-2); border-radius: 8px; padding: .45em; text-align: center; }

/* Layout applicativo */
.app { display: grid; grid-template-columns: 280px minmax(0, 1fr); height: 100vh; height: 100dvh; }
.sidebar { background: var(--sidebar); color: var(--sidebar-text); display: flex; flex-direction: column; min-height: 0; }
.sidebar a { color: inherit; text-decoration: none; }
.sidebar-head { padding: 1rem; display: flex; flex-direction: column; gap: .75rem; }
.sidebar .brand { margin: 0; color: #fff; }
.sidebar .btn { background: transparent; color: var(--sidebar-text); border-color: #2b3c58; }
.sidebar .btn:hover { background: var(--sidebar-hover); }
.sidebar-search input { background: var(--sidebar-hover); border-color: #2b3c58; color: var(--sidebar-text); }
.sidebar-search input::placeholder { color: var(--sidebar-muted); }
.conv-list { flex: 1; overflow-y: auto; padding: 0 .5rem 1rem; list-style: none; margin: 0; }
.conv-list li a { display: block; padding: .5em .75em; border-radius: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: .92rem; }
.conv-list li a:hover { background: var(--sidebar-hover); }
.conv-list li a.active { background: #22375a; color: #fff; }
.conv-list .group { font-size: .74rem; text-transform: uppercase; letter-spacing: .06em; color: var(--sidebar-muted); padding: .9em .75em .3em; }
.sidebar-foot { border-top: 1px solid #22314b; padding: .75rem 1rem; display: flex; flex-direction: column; gap: .25rem; font-size: .9rem; }
.sidebar-foot a, .sidebar-foot button { display: flex; align-items: center; gap: .5em; padding: .45em .5em; border-radius: 8px; color: var(--sidebar-text); background: none; border: 0; font: inherit; cursor: pointer; text-align: left; width: 100%; }
.sidebar-foot a:hover, .sidebar-foot button:hover { background: var(--sidebar-hover); }
.user-chip { display: flex; align-items: center; gap: .6em; padding: .4em .5em; color: var(--sidebar-muted); font-size: .85rem; }
.avatar { width: 28px; height: 28px; border-radius: 50%; background: #2b4470; color: #fff; display: grid; place-items: center; font-weight: 600; font-size: .8rem; flex: none; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar { display: flex; align-items: center; gap: .75rem; padding: .6rem 1rem; border-bottom: 1px solid var(--border); background: var(--surface); min-height: 56px; }
.topbar .title { font-weight: 600; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar select { width: auto; padding: .35em .6em; font-size: .88rem; }
.menu-toggle { display: none; }

/* Chat */
.chat-scroll { flex: 1; overflow-y: auto; }
.chat-inner { max-width: 820px; margin: 0 auto; padding: 1.5rem 1rem 2rem; }
.empty-state { text-align: center; padding: 12vh 1rem 2rem; }
.empty-state h1 { font-size: 1.7rem; }
.suggestions { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .6rem; margin-top: 1.5rem; text-align: left; }
.suggestion { padding: .8em 1em; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); cursor: pointer; font: inherit; color: var(--text); text-align: left; }
.suggestion:hover { background: var(--surface-2); }
.suggestion strong { display: block; font-size: .92rem; }
.suggestion span { font-size: .84rem; color: var(--muted); }

.msg { display: flex; gap: .8rem; margin-bottom: 1.4rem; }
.msg-avatar { width: 30px; height: 30px; border-radius: 8px; flex: none; display: grid; place-items: center; font-size: .75rem; font-weight: 700; }
.msg-user .msg-avatar { background: var(--surface-2); color: var(--muted); }
.msg-assistant .msg-avatar { background: linear-gradient(135deg, var(--primary), var(--accent)); color: #fff; }
.msg-body { min-width: 0; flex: 1; }
.msg-user .msg-content { background: var(--surface-2); padding: .7em 1em; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; display: inline-block; max-width: 100%; }
.msg-content { overflow-wrap: anywhere; }
.msg-content p { margin: 0 0 .8em; }
.msg-content p:last-child { margin-bottom: 0; }
.msg-content pre { background: var(--code-bg); color: var(--code-text); padding: 1em; border-radius: 10px; overflow-x: auto; font-size: .87rem; }
.msg-content :not(pre) > code { background: var(--surface-2); padding: .1em .35em; border-radius: 5px; }
.msg-content table { border-collapse: collapse; width: 100%; margin: .6em 0; font-size: .92rem; display: block; overflow-x: auto; }
.msg-content th, .msg-content td { border: 1px solid var(--border); padding: .4em .6em; text-align: left; }
.msg-content blockquote { border-left: 3px solid var(--border); margin: .6em 0; padding-left: 1em; color: var(--muted); }
.msg-content.streaming { white-space: pre-wrap; }
.msg-content.streaming::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--muted); }
@keyframes blink { 50% { opacity: 0; } }
.msg-status { font-size: .85rem; color: var(--muted); display: flex; align-items: center; gap: .5em; margin-bottom: .4em; }
.spinner { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--border); border-top-color: var(--primary); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.msg-error { color: var(--danger); font-size: .9rem; margin-top: .4em; }
.msg-actions { display: flex; gap: .3rem; margin-top: .4rem; opacity: .7; }
.msg-actions .btn { padding: .2em .55em; font-size: .8rem; }
.msg-attachments { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: .4rem; }
.msg-user .msg-body { display: flex; flex-direction: column; align-items: flex-end; }
.file-chip { display: inline-flex; align-items: center; gap: .4em; padding: .3em .7em; border: 1px solid var(--border); border-radius: 999px; font-size: .84rem; background: var(--surface); color: var(--text); text-decoration: none; max-width: 260px; }
.file-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-chip button { border: 0; background: none; cursor: pointer; color: var(--muted); font-size: 1rem; line-height: 1; padding: 0 0 0 .2em; }
.file-chip.uploading { opacity: .6; }
.generated-image img { max-width: 100%; height: auto; max-height: 560px; width: auto; background: var(--surface-2); border-radius: 12px; border: 1px solid var(--border); display: block; margin: .3rem 0 .5rem; }
.sources { margin-top: .8rem; border-top: 1px dashed var(--border); padding-top: .6rem; font-size: .85rem; }
.sources h4 { margin: 0 0 .3rem; font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.sources ul { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: .25rem; }
.badge { display: inline-block; padding: .1em .55em; border-radius: 999px; font-size: .74rem; font-weight: 600; background: var(--surface-2); color: var(--muted); vertical-align: middle; }
.badge-kb { background: color-mix(in srgb, var(--primary) 15%, var(--surface)); color: var(--primary); }
.badge-web { background: color-mix(in srgb, var(--success) 15%, var(--surface)); color: var(--success); }
.badge-ok { background: color-mix(in srgb, var(--success) 15%, var(--surface)); color: var(--success); }
.badge-warn { background: color-mix(in srgb, var(--warning) 18%, var(--surface)); color: var(--warning); }
.badge-err { background: color-mix(in srgb, var(--danger) 15%, var(--surface)); color: var(--danger); }

/* Composer */
.composer-wrap { padding: .5rem 1rem 1rem; background: linear-gradient(transparent, var(--bg) 30%); }
.composer { max-width: 820px; margin: 0 auto; background: var(--surface); border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow); padding: .6rem .7rem .5rem; }
.composer textarea { border: 0; resize: none; padding: .4em .4em; max-height: 40vh; min-height: 2.6em; background: transparent; }
.composer textarea:focus-visible { outline: none; }
.composer:focus-within { border-color: color-mix(in srgb, var(--primary) 50%, var(--border)); }
.composer-files { display: flex; flex-wrap: wrap; gap: .4rem; padding: 0 .3rem .4rem; }
.composer-files:empty { display: none; }
.composer-bar { display: flex; align-items: center; gap: .35rem; flex-wrap: wrap; }
.composer-bar .spacer { flex: 1; }
.tool-toggle { display: inline-flex; align-items: center; gap: .35em; padding: .35em .7em; border-radius: 999px; border: 1px solid var(--border); font-size: .84rem; cursor: pointer; user-select: none; color: var(--muted); background: var(--surface); }
.tool-toggle[aria-pressed=true] { color: var(--primary); border-color: var(--primary); background: color-mix(in srgb, var(--primary) 10%, var(--surface)); }
.icon-btn { width: 36px; height: 36px; border-radius: 50%; padding: 0; }
.send-btn { width: 38px; height: 38px; border-radius: 50%; padding: 0; }
.composer-note { text-align: center; font-size: .76rem; color: var(--muted); margin-top: .45rem; }
.image-panel { display: grid; grid-template-columns: 1fr auto; gap: .5rem; padding: .3rem .3rem .6rem; border-bottom: 1px solid var(--border); margin-bottom: .4rem; align-items: center; }
.image-panel select { width: auto; }
.image-panel .opts { display: flex; gap: .8rem; align-items: center; flex-wrap: wrap; font-size: .85rem; }
.image-panel .opts label { display: inline-flex; gap: .35em; align-items: center; margin: 0; font-weight: 400; }

/* Area amministrativa */
.admin-main { overflow-y: auto; }
.admin-inner { max-width: 1180px; margin: 0 auto; padding: 1.5rem 1.25rem 3rem; }
.admin-nav { display: flex; gap: .3rem; flex-wrap: wrap; margin-bottom: 1.4rem; border-bottom: 1px solid var(--border); padding-bottom: .6rem; }
.admin-nav a { padding: .45em .85em; border-radius: 8px; text-decoration: none; color: var(--muted); font-weight: 500; font-size: .92rem; }
.admin-nav a:hover { background: var(--surface-2); color: var(--text); }
.admin-nav a.active { background: var(--primary); color: var(--primary-contrast); }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head h1 { margin: 0; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.2rem 1.3rem; margin-bottom: 1.2rem; box-shadow: var(--shadow); }
.card h2 { font-size: 1.02rem; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .9rem; margin-bottom: 1.2rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.1rem; }
.stat .label { font-size: .8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: 1.55rem; font-weight: 700; margin-top: .2rem; font-variant-numeric: tabular-nums; }
.stat .sub { font-size: .82rem; color: var(--muted); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.2rem; }
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.data th, table.data td { padding: .55em .7em; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
table.data th { font-size: .76rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tr:hover td { background: var(--surface-2); }
.bars { display: flex; flex-direction: column; gap: .35rem; }
.bar-row { display: grid; grid-template-columns: 90px 1fr 90px; gap: .6rem; align-items: center; font-size: .84rem; }
.bar-row meter { width: 100%; height: 12px; }
.bar-row .num { text-align: right; font-variant-numeric: tabular-nums; }
.filters { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-bottom: 1rem; }
.filters input, .filters select { width: auto; min-width: 180px; }
.inline-form { display: inline; }
.actions { display: flex; gap: .4rem; flex-wrap: wrap; align-items: center; }
pre.details { white-space: pre-wrap; font-size: .78rem; margin: 0; max-width: 420px; color: var(--muted); }
.pagination { display: flex; gap: .4rem; align-items: center; justify-content: center; margin-top: 1rem; }
.prompt-text { white-space: pre-wrap; font-size: .87rem; background: var(--surface-2); padding: .8em 1em; border-radius: 8px; max-height: 220px; overflow: auto; }
details summary { cursor: pointer; }

.error-page { min-height: 100%; display: grid; place-items: center; text-align: center; padding: 2rem; }
.error-page .code { font-size: 4rem; font-weight: 800; color: var(--muted); }

/* Responsive */
@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .topbar select { max-width: 42%; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filters input, .filters select { min-width: 0; flex: 1; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 86vw; max-width: 320px; z-index: 40; transform: translateX(-100%); transition: transform .2s ease; }
  .app.nav-open .sidebar { transform: none; box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .35); }
  .menu-toggle { display: inline-flex; }
  .grid-2 { grid-template-columns: 1fr; }
  .chat-inner { padding: 1rem .75rem 1.5rem; }
  .composer-wrap { padding: .4rem .5rem .6rem; }
  .msg-avatar { display: none; }
  .image-panel { grid-template-columns: 1fr; }
  .bar-row { grid-template-columns: 70px 1fr 70px; }
  .auth-card { padding: 1.4rem; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
.conv-menu { position: relative; }
.conv-menu summary { list-style: none; }
.conv-menu summary::-webkit-details-marker { display: none; }
.conv-menu .card { position: absolute; right: 0; top: calc(100% + 6px); width: 290px; z-index: 30; margin: 0; }
.conv-menu form + form { margin-top: .6rem; }
.field.checkbox { flex-wrap: wrap; }
.field.checkbox .helptext, .field.checkbox .errorlist { flex-basis: 100%; order: 3; }
/* Voce */
#mic-btn.recording { background: color-mix(in srgb, var(--danger) 15%, var(--surface)); color: var(--danger); animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--danger) 18%, transparent); } }
.rec-indicator { font-size: .82rem; color: var(--danger); font-variant-numeric: tabular-nums; }
.rec-indicator.busy { color: var(--muted); }
[data-speak].playing { color: var(--primary); }
