* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
:root {
    --bg: #f8fafc;
    --surface: #fff;
    --text: #0f172a;
    --muted: #64748b;
    --border: #e2e8f0;
    --primary: #2563eb;
    --primary-dark: #1e40af;
    --success: #16a34a;
    --error: #dc2626;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(0,0,0,0.05);
    --shadow-lg: 0 10px 30px rgba(0,0,0,0.1);
    --tap-size: 44px;
}
@media (prefers-color-scheme: dark) {
    :root { --bg:#0f172a; --surface:#1e293b; --text:#f1f5f9; --muted:#94a3b8; --border:#334155; }
}
html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif; color: var(--text); background: var(--bg); -webkit-font-smoothing: antialiased; overscroll-behavior: none; }
body { display: flex; flex-direction: column; }
.hidden { display: none !important; }

/* Login */
.login-screen { display:flex; align-items:center; justify-content:center; min-height:100vh; padding:20px; background: linear-gradient(135deg, #2563eb 0%, #7c3aed 100%); }
.login-card { background: var(--surface); padding: 32px; border-radius: 16px; width: 100%; max-width: 360px; box-shadow: var(--shadow-lg); }
.login-card .logo { color: var(--primary); display:flex; justify-content:center; margin-bottom: 12px; }
.login-card h1 { font-size: 24px; text-align: center; margin-bottom: 4px; }
.login-card .sub { text-align: center; color: var(--muted); font-size: 13px; margin-bottom: 24px; }
.login-card input { width:100%; padding: 14px; border:1px solid var(--border); border-radius: 8px; font-size: 16px; margin-bottom: 10px; background: var(--bg); color: var(--text); }
.login-card button { width:100%; padding: 14px; background: var(--primary); color: white; border:none; border-radius:8px; font-size:16px; font-weight:600; cursor:pointer; }
.login-card button:hover { background: var(--primary-dark); }
.login-card .error { color: var(--error); font-size: 13px; margin-top: 10px; text-align: center; }

/* App layout */
#app { display: flex; flex-direction: column; height: 100vh; max-height: 100vh; }
header { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; padding-top: env(safe-area-inset-top, 0); }
.header-inner { display:flex; align-items:center; justify-content:space-between; padding: 12px 16px; }
.brand { display:flex; align-items:center; gap:8px; color: var(--primary); font-weight: 700; font-size: 17px; }
.header-actions { display:flex; gap:6px; align-items:center; }
.provider-select { border:1px solid var(--border); border-radius: 8px; padding: 6px 8px; font-size:12px; background: var(--bg); color: var(--text); }
.icon-btn { background: transparent; border:none; padding: 8px 10px; font-size: 18px; cursor: pointer; border-radius: 8px; color: var(--muted); }
.icon-btn:hover { background: var(--bg); color: var(--text); }

main#chat { flex:1; overflow-y:auto; padding: 16px; }
#messages { display: flex; flex-direction: column; gap: 12px; max-width: 900px; margin: 0 auto; }
.welcome { text-align: center; padding: 40px 20px; }
.welcome h2 { margin-bottom: 6px; }
.welcome p { color: var(--muted); margin-bottom: 24px; }
.suggestions { display:flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 16px; }
.suggestions button { background: var(--surface); border: 1px solid var(--border); padding: 10px 14px; border-radius: 20px; font-size: 13px; cursor: pointer; color: var(--text); }
.suggestions button:hover { border-color: var(--primary); color: var(--primary); }
.welcome ul.rules { list-style: none; padding: 0; margin: 16px auto; text-align: left; max-width: 520px; }
.welcome ul.rules li { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; margin-bottom: 8px; font-size: 14px; line-height: 1.45; color: var(--text); }
.welcome .hint { font-size: 12px; color: var(--muted); margin-top: 8px; font-style: italic; }

/* Speak buttons on AI messages */
.msg.ai .speak-btn { background: transparent; border: 1px solid var(--border); padding: 4px 10px; border-radius: 14px; font-size: 12px; cursor: pointer; color: var(--muted); margin-top: 6px; display: inline-flex; align-items: center; gap: 4px; }
.msg.ai .speak-btn:hover { border-color: var(--primary); color: var(--primary); }
.msg.ai .speak-btn.speaking { background: var(--primary); color: white; border-color: var(--primary); }

/* Mobile improvements */
@media (max-width: 640px) {
    .header-inner { padding: 10px 12px; }
    .brand { font-size: 15px; }
    .brand svg { width: 20px; height: 20px; }
    .provider-select { font-size: 11px; padding: 6px; max-width: 130px; }
    .icon-btn { padding: 8px; font-size: 18px; min-width: var(--tap-size); min-height: var(--tap-size); display: flex; align-items: center; justify-content: center; }

    main#chat { padding: 12px; }
    .welcome { padding: 24px 12px; }
    .welcome h2 { font-size: 20px; }
    .welcome p { font-size: 14px; margin-bottom: 12px; }
    .welcome ul.rules li { font-size: 13px; padding: 10px 12px; }
    .suggestions button { font-size: 12px; padding: 10px 12px; min-height: var(--tap-size); }

    .msg { max-width: 94%; padding: 10px 12px; }
    .msg.ai .body { font-size: 14px; }
    .msg.ai .body table { font-size: 12px; display: block; overflow-x: auto; white-space: nowrap; }
    .msg.ai .body th, .msg.ai .body td { padding: 5px 7px; }
    .msg.ai .meta { font-size: 10px; gap: 6px; }

    footer { padding: 6px 8px; padding-bottom: calc(6px + env(safe-area-inset-bottom, 0)); }
    textarea#queryInput { font-size: 16px; padding: 10px 12px; }  /* 16px prevents iOS auto-zoom */
    .send-btn { width: var(--tap-size); height: var(--tap-size); }
    #voiceBtn { min-width: var(--tap-size); min-height: var(--tap-size); }
}

@media (max-width: 380px) {
    .provider-select { max-width: 100px; font-size: 10px; }
    .welcome h2 { font-size: 18px; }
    .suggestions button { font-size: 11px; }
}

.msg { padding: 12px 16px; border-radius: var(--radius); max-width: 90%; word-wrap: break-word; animation: fadeIn 0.2s ease; }
.msg.user { align-self: flex-end; background: var(--primary); color: white; }
.msg.ai { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); }
.msg.ai .meta { font-size: 11px; color: var(--muted); margin-bottom: 6px; display:flex; gap: 10px; flex-wrap: wrap; }
.msg.ai .meta span { display:inline-flex; align-items:center; gap:3px; }
.msg.ai .body { font-size: 15px; line-height: 1.5; }
.msg.ai .body pre { background: var(--bg); padding: 10px; border-radius: 6px; overflow-x: auto; font-size: 12px; }
.msg.ai .body table { border-collapse: collapse; width: 100%; margin: 8px 0; font-size: 13px; }
.msg.ai .body th, .msg.ai .body td { border: 1px solid var(--border); padding: 6px 8px; text-align: left; }
.msg.ai .body th { background: var(--bg); font-weight: 600; }
.msg.ai .tool-calls { margin-top: 8px; padding: 6px 10px; background: var(--bg); border-radius: 6px; font-size: 11px; color: var(--muted); }
.msg.ai .actions { margin-top: 8px; display: flex; gap: 6px; flex-wrap: wrap; }
.msg.ai .actions button { background: var(--bg); border: 1px solid var(--border); padding: 6px 10px; border-radius: 6px; font-size: 12px; cursor: pointer; color: var(--text); }
.msg.loading { background: var(--surface); border: 1px solid var(--border); align-self: flex-start; }
.msg.loading .dots { display:inline-flex; gap: 4px; }
.msg.loading .dots span { width: 8px; height: 8px; border-radius: 50%; background: var(--primary); animation: pulse 1.4s infinite ease-in-out; }
.msg.loading .dots span:nth-child(2) { animation-delay: 0.2s; }
.msg.loading .dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes pulse { 0%, 60%, 100% { transform: scale(0.6); opacity: 0.4; } 30% { transform: scale(1); opacity: 1; } }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }

footer { background: var(--surface); border-top: 1px solid var(--border); padding: 8px 12px; padding-bottom: calc(8px + env(safe-area-inset-bottom, 0)); }
.input-row { display:flex; gap: 6px; align-items: flex-end; max-width: 900px; margin: 0 auto; }
textarea#queryInput { flex: 1; border: 1px solid var(--border); border-radius: 20px; padding: 10px 14px; font-size: 15px; resize: none; font-family: inherit; max-height: 120px; background: var(--bg); color: var(--text); }
textarea#queryInput:focus { outline: none; border-color: var(--primary); }
.send-btn { background: var(--primary); color: white; border: none; width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; flex-shrink: 0; }
.send-btn:disabled { background: var(--muted); cursor: not-allowed; }

.history-panel { position: fixed; right: 0; top: 0; height: 100%; width: 90%; max-width: 360px; background: var(--surface); box-shadow: -10px 0 30px rgba(0,0,0,0.15); z-index: 20; overflow-y: auto; transform: translateX(100%); transition: transform 0.2s; }
.history-panel:not(.hidden) { transform: translateX(0); }
.history-header { display:flex; align-items:center; justify-content:space-between; padding: 16px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); }
.history-item { padding: 12px 16px; border-bottom: 1px solid var(--border); cursor: pointer; }
.history-item:hover { background: var(--bg); }
.history-item .q { font-size: 14px; font-weight: 500; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.history-item .time { font-size: 11px; color: var(--muted); margin-top: 4px; }

/* Mic permission banner */
.mic-banner { background: linear-gradient(135deg, #fbbf24, #f59e0b); color: #1f2937; padding: 10px 14px; display: flex; align-items: center; gap: 10px; font-size: 14px; box-shadow: 0 2px 6px rgba(0,0,0,0.1); }
.mic-banner span { flex: 1; }
.mic-allow-btn { background: #1f2937; color: white; border: none; padding: 8px 16px; border-radius: 8px; font-weight: 600; cursor: pointer; font-size: 13px; }
.mic-allow-btn:hover { background: #111827; }
.mic-dismiss-btn { background: transparent; border: none; color: #1f2937; font-size: 20px; cursor: pointer; padding: 4px 8px; }
@media (max-width: 640px) {
    .mic-banner { font-size: 12px; padding: 8px 10px; }
    .mic-banner span { font-size: 12px; }
    .mic-allow-btn { padding: 6px 10px; font-size: 11px; }
}

/* Wialon login extras */
.login-sep { display:flex; align-items:center; margin: 16px 0; color: var(--muted); font-size: 12px; gap: 8px; }
.login-sep::before, .login-sep::after { content: ''; flex: 1; height: 1px; background: var(--border); }
.btn-wialon { width: 100%; padding: 12px; background: transparent; color: var(--primary); border: 1px solid var(--primary); border-radius: 8px; font-size: 14px; font-weight: 600; cursor: pointer; }
.btn-wialon:hover { background: rgba(37,99,235,0.08); }
#wialonForm { margin-top: 10px; }
#wialonForm input { width:100%; padding: 14px; border:1px solid var(--border); border-radius: 8px; font-size: 16px; margin-bottom: 10px; background: var(--bg); color: var(--text); font-family: monospace; font-size: 12px; }
