:root {
  --bg: #0b1020;
  --surface: #131a2e;
  --surface-2: #1a233d;
  --border: rgba(148, 163, 184, 0.16);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-strong: #0ea5e9;
  --user-bg: #2563eb;
  --success: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --radius: 16px;
}

body[data-engine="local"] {
  --bg: #061713;
  --surface: #0d2620;
  --surface-2: #13352c;
  --accent: #34d399;
  --accent-strong: #10b981;
  --user-bg: #059669;
}

body[data-engine="local"] .login-screen {
  background: radial-gradient(circle at 50% 0%, #064e3b 0%, #061713 65%);
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang TC", "Microsoft JhengHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}

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

.hidden {
  display: none !important;
}

.screen {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
}

/* ===== Lock Screen ===== */
.login-screen {
  justify-content: center;
  align-items: center;
  padding: calc(20px + env(safe-area-inset-top)) 20px calc(20px + env(safe-area-inset-bottom));
  background: radial-gradient(circle at 50% 0%, #172554 0%, #0b1020 65%);
}

.login-card {
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.login-logo {
  width: 64px;
  height: 64px;
  border-radius: 16px;
  margin-bottom: 12px;
  box-shadow: 0 10px 30px rgba(14, 165, 233, 0.25);
}

.login-title {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.2px;
}

.login-sub {
  margin: 6px 0 18px;
  font-size: 13px;
  color: var(--muted);
}

.pin-dots {
  display: flex;
  gap: 14px;
  margin-bottom: 10px;
}

.pin-dot {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  border: 2px solid rgba(148, 163, 184, 0.5);
  transition: transform 0.12s ease, background-color 0.12s ease, border-color 0.12s ease;
}

.pin-dot.filled {
  background: var(--accent);
  border-color: var(--accent);
  transform: scale(1.1);
}

.pin-dots.shake {
  animation: shake 0.35s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-6px); }
  80% { transform: translateX(6px); }
}

.login-error {
  min-height: 20px;
  font-size: 13px;
  color: #fca5a5;
  margin-bottom: 14px;
  text-align: center;
}

.keypad {
  display: grid;
  grid-template-columns: repeat(3, 74px);
  gap: 12px 18px;
}

.key {
  width: 74px;
  height: 62px;
  border-radius: 31px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  font-size: 26px;
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.key:active {
  background: rgba(56, 189, 248, 0.28);
  transform: scale(0.95);
}

.key-fn {
  font-size: 15px;
  color: var(--muted);
  background: transparent;
  border-color: transparent;
}

/* ===== Main App Shell ===== */
.app-screen {
  background: var(--bg);
}

.topbar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 10px 8px;
  background: rgba(11, 16, 32, 0.9);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  z-index: 10;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: none;
  background: transparent;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
}

.icon-btn:active {
  background: rgba(255, 255, 255, 0.1);
}

.topbar-center {
  flex: 1;
  min-width: 0;
  text-align: center;
  padding: 2px 6px;
  border-radius: 10px;
  cursor: pointer;
}

.topbar-center:active {
  background: rgba(255, 255, 255, 0.06);
}

.topbar-title {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-meta {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  display: inline-block;
}

.status-dot.ok { background: var(--success); }
.status-dot.running { background: var(--accent); animation: pulse 1.1s infinite; }
.status-dot.warn { background: var(--warn); animation: pulse 0.9s infinite; }
.status-dot.err { background: var(--danger); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

/* ===== Chat Feed ===== */
.chat-feed {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 12px 18px;
}

.empty-state {
  margin: 26px auto 12px;
  max-width: 330px;
  text-align: center;
}

.empty-logo {
  width: 56px;
  height: 56px;
  border-radius: 14px;
  margin-bottom: 10px;
}

.empty-state h2 {
  margin: 0 0 6px;
  font-size: 18px;
  font-weight: 700;
}

.empty-sub {
  margin: 0 0 18px;
  font-size: 12px;
  color: var(--muted);
}

.quick-chips {
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.chip {
  width: 100%;
  padding: 12px 14px;
  border-radius: 13px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  text-align: left;
  cursor: pointer;
}

.chip:active {
  background: var(--surface-2);
}

.messages-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  display: flex;
  flex-direction: column;
  max-width: 100%;
}

.msg-user {
  align-items: flex-end;
}

.msg-assistant {
  align-items: flex-start;
}

.bubble {
  max-width: 88%;
  padding: 10px 13px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.55;
  word-break: break-word;
}

.msg-user .bubble {
  background: var(--user-bg);
  color: #ffffff;
  border-bottom-right-radius: 5px;
  white-space: pre-wrap;
}

.msg-assistant .bubble {
  max-width: 96%;
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
}

/* Markdown inside assistant bubble */
.md-body p { margin: 0 0 8px; }
.md-body p:last-child { margin-bottom: 0; }
.md-body h1, .md-body h2, .md-body h3 {
  margin: 10px 0 6px;
  font-size: 15px;
  font-weight: 700;
  color: #e2e8f0;
}
.md-body ul, .md-body ol {
  margin: 4px 0 8px;
  padding-left: 20px;
}
.md-body li { margin-bottom: 3px; }
.md-body a { color: var(--accent); word-break: break-all; }
.md-body code.inline-code {
  background: rgba(148, 163, 184, 0.16);
  padding: 1px 5px;
  border-radius: 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  color: #7dd3fc;
}

.code-block {
  margin: 8px 0;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: #070b14;
}

.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 10px;
  background: rgba(255, 255, 255, 0.04);
  font-size: 11px;
  color: var(--muted);
}

.copy-btn {
  border: none;
  background: rgba(56, 189, 248, 0.15);
  color: var(--accent);
  border-radius: 6px;
  padding: 3px 8px;
  font-size: 11px;
  cursor: pointer;
}

.code-block pre {
  margin: 0;
  padding: 10px;
  overflow-x: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.45;
  color: #e2e8f0;
}

/* Thought & Tool disclosures */
.thought-box, .tools-box {
  margin-bottom: 8px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.035);
  border: 1px solid rgba(148, 163, 184, 0.12);
  font-size: 12.5px;
}

.thought-box summary, .tools-box summary {
  padding: 7px 10px;
  color: var(--muted);
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 6px;
}

.thought-content {
  padding: 0 10px 8px;
  color: #94a3b8;
  white-space: pre-wrap;
  max-height: 200px;
  overflow-y: auto;
  font-size: 12px;
  line-height: 1.45;
}

.tool-item {
  padding: 6px 10px;
  border-top: 1px solid rgba(148, 163, 184, 0.1);
  display: flex;
  align-items: flex-start;
  gap: 7px;
  font-size: 12px;
}

.tool-name {
  font-weight: 600;
  color: #cbd5e1;
  word-break: break-all;
}

.tool-detail {
  color: var(--muted);
  font-family: ui-monospace, Menlo, monospace;
  font-size: 11px;
  margin-top: 2px;
  word-break: break-all;
}

.typing-cursor {
  display: inline-block;
  width: 8px;
  height: 15px;
  background: var(--accent);
  vertical-align: middle;
  margin-left: 3px;
  animation: blink 0.9s infinite;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* ===== Permission Approval Cards ===== */
.permissions-list {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.perm-card {
  background: linear-gradient(180deg, rgba(245, 158, 11, 0.14), rgba(19, 26, 46, 0.98));
  border: 1.5px solid rgba(245, 158, 11, 0.65);
  border-radius: 16px;
  padding: 13px;
}

.perm-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 14px;
  color: #fcd34d;
  margin-bottom: 6px;
}

.perm-tool {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
}

.perm-cmd {
  background: #070b14;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 12px;
  color: #e2e8f0;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 150px;
  overflow-y: auto;
  margin-bottom: 10px;
}

.perm-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.perm-btn {
  flex: 1;
  min-width: 110px;
  min-height: 46px;
  border-radius: 12px;
  border: none;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
}

.perm-btn.allow {
  background: var(--success);
  color: #052e16;
}

.perm-btn.deny {
  background: rgba(239, 68, 68, 0.2);
  border: 1px solid rgba(239, 68, 68, 0.5);
  color: #fca5a5;
}

.perm-btn.neutral {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
}

.perm-btn:disabled {
  opacity: 0.45;
}

/* ===== Bottom Composer ===== */
.composer-wrap {
  flex-shrink: 0;
  background: rgba(11, 16, 32, 0.96);
  border-top: 1px solid var(--border);
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
}

.image-preview-row {
  display: flex;
  gap: 8px;
  padding-bottom: 8px;
  overflow-x: auto;
}

.img-thumb-wrap {
  position: relative;
  width: 56px;
  height: 56px;
  flex-shrink: 0;
}

.img-thumb {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--border);
}

.img-remove {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: none;
  background: var(--danger);
  color: #fff;
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 5px 6px 5px 8px;
}

.composer-icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  cursor: pointer;
}

.prompt-input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 16px; /* Critical: >=16px prevents iOS Safari zoom on focus */
  line-height: 1.4;
  max-height: 120px;
  resize: none;
  outline: none;
  padding: 7px 2px;
}

.prompt-input::placeholder {
  color: #64748b;
}

.send-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: var(--accent-strong);
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  cursor: pointer;
}

.send-btn.stop-mode {
  background: var(--danger);
}

.send-btn:disabled {
  opacity: 0.4;
}

/* ===== Drawer & Bottom Sheet ===== */
.backdrop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.58);
  z-index: 30;
}

.drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 82%;
  max-width: 310px;
  background: #0f1629;
  border-right: 1px solid var(--border);
  z-index: 40;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 12px 10px 16px;
  border-bottom: 1px solid var(--border);
}

.drawer-title {
  font-size: 16px;
  font-weight: 700;
}

.drawer-sub {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}

.drawer-actions {
  padding: 12px;
}

.primary-btn {
  width: 100%;
  padding: 11px;
  border-radius: 12px;
  border: none;
  background: var(--accent-strong);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.session-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0 10px 12px;
}

.session-item {
  width: 100%;
  padding: 11px 12px;
  margin-bottom: 6px;
  border-radius: 12px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.session-item.active {
  background: rgba(56, 189, 248, 0.14);
  border-color: rgba(56, 189, 248, 0.35);
}

.session-item-title {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item-time {
  font-size: 11px;
  color: var(--muted);
}

.drawer-footer {
  padding: 12px;
  border-top: 1px solid var(--border);
}

.danger-outline-btn {
  width: 100%;
  padding: 10px;
  border-radius: 12px;
  border: 1px solid rgba(239, 68, 68, 0.45);
  background: transparent;
  color: #fca5a5;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.bottom-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #11192e;
  border-top: 1px solid var(--border);
  border-radius: 20px 20px 0 0;
  z-index: 40;
  padding: 8px 16px calc(18px + env(safe-area-inset-bottom));
}

.sheet-handle {
  width: 38px;
  height: 4px;
  border-radius: 2px;
  background: rgba(148, 163, 184, 0.35);
  margin: 4px auto 10px;
}

.sheet-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.sheet-header h3 {
  margin: 0;
  font-size: 16px;
}

.sheet-label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin: 10px 0 6px;
}

.sheet-select {
  width: 100%;
  padding: 11px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 16px;
}

.usage-info {
  margin-top: 14px;
  font-size: 12px;
  color: var(--muted);
}

.toast {
  position: fixed;
  top: calc(64px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 60;
  max-width: 90%;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}
