:root {
  --bg: #0d1117;
  --surface: #161b22;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #238636;
  --accent-hover: #2ea043;
  --user-bubble: #21262d;
  --assistant-bubble: #1a2332;
  --danger: #f85149;
  --tab-h: 52px;
  --top-h: 48px;
  --min-tap: 44px;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  height: 100%;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  background: var(--bg);
  color: var(--text);
}

.hidden {
  display: none !important;
}

.view {
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}

#view-main {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  padding-bottom: var(--tab-h);
}

.login-card {
  max-width: 360px;
  margin: 15vh auto;
  padding: 1.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.login-card h1 {
  margin: 0 0 0.5rem;
  font-size: 1.25rem;
}

.hint {
  color: var(--muted);
  font-size: 0.875rem;
  margin-bottom: 1rem;
}

#login-token {
  width: 100%;
  padding: 12px;
  margin-bottom: 1rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}

.err {
  color: var(--danger);
  font-size: 0.875rem;
  margin-top: 0.5rem;
}

.btn {
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 1rem;
  cursor: pointer;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  width: 100%;
}

.btn.primary:active {
  background: var(--accent-hover);
}

.btn.small {
  padding: 8px 12px;
  font-size: 0.875rem;
  width: auto;
}

.touch {
  min-height: var(--min-tap);
  min-width: var(--min-tap);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--top-h);
  padding: 0 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.ws-name {
  font-weight: 600;
  font-size: 0.9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 80%;
}

.sse-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--danger);
  flex-shrink: 0;
}

.sse-dot.ok {
  background: var(--accent);
}

.panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

#panel-chat {
  padding: 8px;
  gap: 8px;
}

.chat-bridge-hint {
  margin: 0 4px 4px;
  padding: 8px 10px;
  font-size: 0.8rem;
  line-height: 1.35;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  flex-shrink: 0;
}

.chat-bridge-hint strong {
  color: var(--text);
  font-weight: 600;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 8px 4px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.bubble {
  max-width: 92%;
  padding: 10px 12px;
  border-radius: 12px;
  line-height: 1.45;
  word-break: break-word;
}

.bubble.user {
  align-self: flex-end;
  background: var(--user-bubble);
  border: 1px solid var(--border);
}

.bubble.assistant {
  align-self: flex-start;
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
}

.bubble pre, .bubble code {
  background: var(--bg);
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 0.85em;
  overflow-x: auto;
}

.bubble pre {
  padding: 8px;
  display: block;
  white-space: pre-wrap;
}

.chat-input-row {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  gap: 8px;
  align-items: stretch;
  flex-shrink: 0;
}

#chat-input {
  flex: 1 1 auto;
  min-width: 0;
  width: 0;
  min-height: 5.5rem;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
  resize: vertical;
  font-family: inherit;
  line-height: 1.4;
}

/* .btn.primary ha width:100% globale: qui serve un pulsante compatto */
#chat-send.chat-send-btn.btn.primary {
  width: auto;
  max-width: 7rem;
  flex: 0 0 auto;
  align-self: stretch;
  min-width: 4.75rem;
  padding-left: 14px;
  padding-right: 14px;
}

@media (max-width: 360px) {
  .chat-input-row {
    flex-direction: column;
    align-items: stretch;
  }
  #chat-input {
    width: 100%;
    min-height: 6rem;
  }
  #chat-send.chat-send-btn.btn.primary {
    max-width: none;
    width: 100%;
    min-height: var(--min-tap);
  }
}

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--tab-h);
  padding-bottom: env(safe-area-inset-bottom);
  display: flex;
  border-top: 1px solid var(--border);
  background: var(--surface);
  z-index: 10;
}

.tab {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
}

.tab.active {
  color: var(--accent);
  border-top: 2px solid var(--accent);
}

.files-split {
  display: flex;
  flex: 1;
  min-height: 0;
  flex-direction: column;
}

@media (min-width: 768px) {
  .files-split {
    flex-direction: row;
  }
  .files-browser {
    width: 32%;
    max-width: 280px;
    border-right: 1px solid var(--border);
  }
  .files-editor-wrap {
    flex: 1;
  }
  .phone-only {
    display: none !important;
  }
}

.files-browser {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 8px;
  overflow: hidden;
}

.breadcrumb {
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 8px;
  word-break: break-all;
}

.breadcrumb span {
  cursor: pointer;
  color: var(--accent);
}

.files-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}

.files-list li {
  padding: 12px 8px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--min-tap);
}

.files-list li:active {
  background: var(--user-bubble);
}

.files-list .icon-dir::before {
  content: "📁 ";
}
.files-list .icon-file::before {
  content: "📄 ";
}

.files-editor-wrap {
  display: flex;
  flex-direction: column;
  min-height: 0;
  flex: 1;
  border-top: 1px solid var(--border);
}

@media (min-width: 768px) {
  .files-editor-wrap {
    border-top: none;
  }
}

.files-editor-wrap.hidden {
  display: none !important;
}

.editor-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.editor-title {
  flex: 1;
  font-size: 0.85rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dirty {
  color: var(--danger);
  font-size: 1.2rem;
}

.toggle {
  font-size: 0.75rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 4px;
}

.editor-mount {
  flex: 1;
  min-height: 220px;
  min-width: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 0;
  background: #212121;
}

.editor-mount .CodeMirror {
  height: 100%;
  font-size: 13px;
  font-family: ui-monospace, "Cascadia Code", "Consolas", monospace;
}

.editor-mount .CodeMirror-scroll {
  min-height: 200px;
}

.editor-save {
  margin: 8px;
  width: calc(100% - 16px);
}

@media (min-width: 768px) {
  .editor-save {
    width: auto;
    align-self: flex-start;
    margin-left: 8px;
  }
}

.files-split.editor-full .files-browser {
  display: none;
}

.files-split.editor-full .files-editor-wrap {
  display: flex !important;
  border: none;
}
