:root {
  --bg: #1b1a2e;
  --panel: #25233d;
  --panel-2: #2f2c4d;
  --line: #3d3960;
  --text: #f1effa;
  --muted: #a7a2c7;
  --accent: #7c6cf0;
  --accent-2: #9d90ff;
  --ok: #6ee7a0;
  --warn: #ffd84a;
  --bad: #ff7b7b;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { font: 15px/1.45 Inter, system-ui, sans-serif; overscroll-behavior: none; }
[hidden] { display: none !important; }

.screen { position: fixed; inset: 0; display: flex; flex-direction: column; }

/* ---------- botones y campos ---------- */
.btn {
  font: 600 14px Inter, system-ui, sans-serif; color: var(--text);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 14px; cursor: pointer; white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); }
.btn.danger { background: #b94a5a; border-color: #b94a5a; }
.btn.ghost { background: transparent; }
.btn.danger.ghost { color: var(--bad); border-color: #6b3342; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; }
.icon-btn {
  background: none; border: 0; color: var(--text); font-size: 26px; line-height: 1;
  width: 40px; height: 40px; border-radius: 10px; cursor: pointer;
}
.icon-btn:active { background: var(--panel-2); }
input, select, textarea {
  font: 16px Inter, system-ui, sans-serif; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.error-text { color: var(--bad); min-height: 1.2em; margin: 8px 0 0; font-size: 13px; }

/* ---------- acceso ---------- */
#login { align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 100%; max-width: 340px; text-align: center; }
.login-logo { width: 88px; height: 88px; image-rendering: pixelated; border-radius: 20px; }
.login-card h1 { font: 600 34px "Pixelify Sans", monospace; margin: 12px 0 4px; letter-spacing: 2px; }
.login-card p { color: var(--muted); margin: 0 0 18px; }
#login-form { display: flex; gap: 8px; }
#login-key { text-align: center; letter-spacing: 4px; font-weight: 600; text-transform: uppercase; }

/* ---------- oficina ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: calc(var(--safe-top) + 10px) 16px 10px; background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.brand { font: 600 20px "Pixelify Sans", monospace; letter-spacing: 3px; }
.summary { flex: 1; display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.chip {
  font: 600 12px Inter, sans-serif; padding: 3px 8px; border-radius: 999px;
  background: var(--panel-2); color: var(--muted);
}
.chip.working { color: #9cc8ff; }
.chip.needs { color: #2a2233; background: var(--warn); }
.chip.done { color: var(--ok); }
.chip.error { color: var(--bad); }
.conn { width: 9px; height: 9px; border-radius: 50%; background: var(--bad); flex: none; }
.conn.on { background: var(--ok); }
.fs-btn {
  background: var(--panel-2); color: var(--text); border: 0; border-radius: 999px;
  width: 32px; height: 32px; font-size: 16px; line-height: 1; cursor: pointer; flex: none;
}
.hire-btn {
  font: 600 13px Inter, system-ui, sans-serif; color: var(--text); background: var(--accent);
  border: 0; border-radius: 999px; padding: 6px 12px; cursor: pointer; white-space: nowrap;
}
#office-scroll { flex: 1; overflow-x: auto; overflow-y: hidden; background: #f1f2f4; scrollbar-width: none; }
#office-scroll::-webkit-scrollbar { display: none; }
#office-canvas { display: block; touch-action: pan-x; }

/* ---------- joystick y botón de hablar ---------- */
#joy {
  position: fixed; left: 22px; bottom: calc(var(--safe-bottom) + 22px); width: 112px; height: 112px;
  border-radius: 50%; background: rgba(20, 20, 30, .18); border: 2px solid rgba(255, 255, 255, .55);
  backdrop-filter: blur(4px); touch-action: none; z-index: 5;
}
#joy-knob {
  position: absolute; left: 50%; top: 50%; width: 48px; height: 48px; margin: -24px 0 0 -24px;
  border-radius: 50%; background: rgba(255, 255, 255, .9); box-shadow: 0 2px 8px rgba(0, 0, 0, .25);
}
.talk-btn {
  position: fixed; right: 18px; bottom: calc(var(--safe-bottom) + 44px); z-index: 5;
  font: 600 15px Inter, system-ui, sans-serif; color: #fff; background: var(--accent);
  border: 0; border-radius: 999px; padding: 14px 18px; box-shadow: 0 4px 14px rgba(0, 0, 0, .3);
  max-width: calc(100vw - 170px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  animation: pop .18s ease-out;
}
@keyframes pop { from { transform: scale(.8); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- hojas (chat / editor) ---------- */
.sheet {
  position: fixed; inset: 0; background: var(--bg); display: flex; flex-direction: column;
  animation: slide-up .22s ease-out; z-index: 10;
}
@keyframes slide-up { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }

.chat-head {
  display: flex; align-items: center; gap: 10px;
  padding: calc(var(--safe-top) + 8px) 8px 8px; border-bottom: 1px solid var(--line); background: var(--panel);
}
#chat-avatar { width: 38px; height: 38px; }
.chat-title { flex: 1; min-width: 0; }
#chat-name { font: 600 17px "Pixelify Sans", monospace; letter-spacing: .5px; }
#chat-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

#chat-log { flex: 1; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 88%; padding: 9px 12px; border-radius: var(--radius); overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--accent); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--panel-2); border-bottom-left-radius: 4px; }
.msg.assistant p { margin: 0 0 8px; }
.msg.assistant p:last-child { margin-bottom: 0; }
.msg.assistant pre {
  background: var(--bg); border-radius: 8px; padding: 8px 10px; overflow-x: auto;
  font: 12.5px/1.4 ui-monospace, Consolas, monospace; margin: 6px 0; white-space: pre;
}
.msg code { font: 13px ui-monospace, Consolas, monospace; background: rgba(0,0,0,.25); padding: 1px 4px; border-radius: 4px; }
.msg pre code { background: none; padding: 0; }
.msg.tool {
  align-self: flex-start; font: 12.5px ui-monospace, Consolas, monospace; color: var(--muted);
  padding: 2px 4px; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.msg.tool b { color: var(--accent-2); font-weight: 600; }
.msg.system { align-self: center; font-size: 12px; color: var(--muted); }
.msg.error { align-self: flex-start; background: #4a2533; color: #ffc7cf; white-space: pre-wrap; }
.msg.permission { align-self: flex-start; background: #4a4020; color: var(--warn); }
.typing { align-self: flex-start; color: var(--muted); font-size: 13px; padding: 2px 4px; }
.empty-chat { margin: auto; text-align: center; color: var(--muted); max-width: 260px; }

.perm-bar {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px;
  background: #3a3320; color: var(--warn); font-size: 13px; border-top: 1px solid #5a4d20;
}
.perm-bar span { flex: 1; }

#chat-form {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px 12px calc(var(--safe-bottom) + 10px); border-top: 1px solid var(--line); background: var(--panel);
}
#chat-input { resize: none; max-height: 140px; }

/* ---------- editor ---------- */
.modal { background: rgba(10, 9, 20, .7); align-items: center; justify-content: center; padding: 16px; }
.modal-card {
  width: 100%; max-width: 420px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 18px; padding: 18px; max-height: 100%; overflow-y: auto;
}
.modal-card h2 { font: 600 22px "Pixelify Sans", monospace; margin: 0 0 12px; }
.modal-card label { display: block; font-size: 13px; color: var(--muted); margin-top: 12px; }
.modal-card label input, .modal-card label select { margin-top: 4px; }
.modal-card input:disabled { opacity: .6; }
.note { font-size: 12.5px; color: var(--muted); margin: 8px 0 0; }
.look-row { display: flex; align-items: center; gap: 14px; }
#editor-avatar { width: 64px; height: 64px; }
.modal-actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.spacer { flex: 1; }
