:root {
  --bg: #0d1117;
  --panel: #161b22;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #1b4332;
  --accent-light: #2d6a4f;
  --primary: #40916c;
  --danger: #f85149;
  --font: "Segoe UI", Tahoma, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); }

.studio-app {
  display: grid;
  grid-template-rows: 48px 1fr 220px;
  height: 100vh;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  background: var(--accent);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand-icon { font-size: 1.4rem; }
.brand small { display: block; color: #b7e4c7; font-size: 0.72rem; }

.topbar-actions { display: flex; align-items: center; gap: 8px; }

.topbar-tools {
  display: flex;
  align-items: center;
  gap: 8px;
}

.pill {
  font-size: 0.75rem;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(255,255,255,0.12);
}
.pill.ok { background: #238636; }
.pill.err { background: var(--danger); }

.workspace {
  display: grid;
  grid-template-columns: 220px 1fr 340px;
  min-height: 0;
  border-bottom: 1px solid var(--border);
}

.sidebar, .agent-pane {
  background: var(--panel);
  border-inline-start: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar { border-inline-start: none; border-inline-end: 1px solid var(--border); }

.sidebar-head, .agent-head, .terminal-head {
  padding: 8px 12px;
  font-size: 0.85rem;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  background: rgba(255,255,255,0.03);
}

.tree {
  list-style: none;
  margin: 0;
  padding: 8px;
  overflow: auto;
  flex: 1;
}

.tree li { margin: 2px 0; }
.tree button {
  width: 100%;
  text-align: start;
  background: none;
  border: none;
  color: var(--text);
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.82rem;
}
.tree button:hover, .tree button.active { background: rgba(64,145,108,0.2); }
.tree .dir { color: #79c0ff; }
.tree .file { color: var(--muted); }

.editor-pane { display: flex; flex-direction: column; min-height: 0; background: #0d1117; }
.tabs {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.tab { font-size: 0.85rem; color: var(--muted); }
.tab.active { color: var(--text); }
.editor { flex: 1; min-height: 0; }

.agent-pane .chat-messages {
  flex: 1;
  overflow: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg {
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 0.86rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.user { background: rgba(64,145,108,0.18); align-self: flex-start; max-width: 95%; }
.msg.bot { background: rgba(255,255,255,0.05); align-self: stretch; }
.msg.tool { background: rgba(121,192,255,0.08); font-size: 0.78rem; color: var(--muted); }
.msg.error { background: rgba(248,81,73,0.15); color: #ffb4ae; }

.chat-form {
  padding: 10px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.chat-form textarea {
  width: 100%;
  resize: vertical;
  min-height: 72px;
  background: #0d1117;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-family: inherit;
}
.chat-actions { display: flex; gap: 8px; }

.terminal-pane {
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.terminal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
#terminal { flex: 1; padding: 4px 8px 8px; min-height: 0; }

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 0.82rem;
}
.btn:hover { border-color: var(--primary); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 0.75rem; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

@media (max-width: 1100px) {
  .workspace { grid-template-columns: 180px 1fr 280px; }
}

/* ── Mobile: تبويبات Editor / Chat / Terminal ── */
.mobile-tabs { display: none; }
.mobile-only { display: none !important; }

@media (max-width: 768px) {
  html, body {
    height: 100%;
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
  }

  .studio-app {
    grid-template-rows: auto 1fr auto;
    height: 100dvh;
    min-height: 100dvh;
  }

  .topbar { grid-row: 1; grid-column: 1; }
  .workspace { grid-row: 2; grid-column: 1; }
  .terminal-pane { grid-row: 2; grid-column: 1; }
  .mobile-tabs { grid-row: 3; grid-column: 1; }

  .topbar {
    flex-wrap: wrap;
    gap: 6px;
    padding: 6px 10px;
    min-height: 48px;
  }

  .brand small { display: none; }

  .topbar-actions {
    flex: 1;
    min-width: 0;
    justify-content: flex-end;
    gap: 6px;
  }

  .mobile-only { display: inline-flex !important; }

  .topbar-tools {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    inset-inline-end: 8px;
    z-index: 50;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    padding: 10px;
    min-width: 200px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
  }

  .topbar-tools.open { display: flex; }

  .topbar-actions { position: relative; }

  .topbar-tools .btn {
    width: 100%;
    justify-content: flex-start;
    text-align: start;
  }

  .auth-user { max-width: 88px; }
  .pill { font-size: 0.68rem; padding: 3px 8px; }

  .workspace,
  .terminal-pane {
    min-height: 0;
    overflow: hidden;
  }

  .workspace {
    display: grid;
    grid-template-columns: 1fr;
    border-bottom: none;
  }

  .terminal-pane {
    border-top: none;
    background: var(--panel);
  }

  /* إخفاء الكل افتراضياً — يُفعَّل حسب التبويب */
  .sidebar,
  .editor-pane,
  .agent-pane,
  .terminal-pane {
    display: none !important;
    min-height: 0;
  }

  .studio-app[data-mobile-tab="files"] .sidebar {
    display: flex !important;
    height: 100%;
    border-inline-end: none;
  }

  .studio-app[data-mobile-tab="files"] .tree button {
    padding: 10px 12px;
    font-size: 0.88rem;
  }

  .studio-app[data-mobile-tab="files"] .workspace {
    grid-template-rows: 1fr;
  }

  .studio-app[data-mobile-tab="editor"] .editor-pane {
    display: flex !important;
    height: 100%;
  }

  .studio-app[data-mobile-tab="editor"] .workspace {
    grid-template-rows: 1fr;
  }

  .studio-app[data-mobile-tab="chat"] .agent-pane {
    display: flex !important;
    border-inline-start: none;
    height: 100%;
  }

  .studio-app[data-mobile-tab="chat"] .workspace {
    grid-template-rows: 1fr;
  }

  .studio-app[data-mobile-tab="terminal"] .terminal-pane {
    display: flex !important;
    flex-direction: column;
    height: 100%;
  }

  .agent-head span:last-child { display: none; }

  .chat-form textarea {
    min-height: 56px;
    font-size: 16px; /* يمنع zoom على iOS */
  }

  .mobile-tabs {
    display: flex;
    grid-row: 3;
    border-top: 1px solid var(--border);
    background: var(--panel);
    padding: 4px 6px calc(4px + env(safe-area-inset-bottom, 0px));
    gap: 4px;
    z-index: 30;
  }

  .mobile-tab {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1px;
    padding: 6px 2px;
    border: none;
    border-radius: 10px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    font-family: inherit;
    font-size: 0.62rem;
    transition: background 0.15s, color 0.15s;
    min-width: 0;
  }

  .mobile-tab-icon { font-size: 1.05rem; line-height: 1; }

  .mobile-tab-label {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .mobile-tab.active {
    background: rgba(64, 145, 108, 0.2);
    color: var(--text);
  }

  .mobile-tab.active .mobile-tab-label { color: var(--primary); font-weight: 600; }

  .composer-modal { max-width: 100vw; width: 100vw; margin: 0; }
  .composer-wrap {
    grid-template-columns: 1fr;
    min-height: 85dvh;
  }
  .composer-files {
    border-inline-end: none;
    border-bottom: 1px solid var(--border);
    max-height: 120px;
  }
  .composer-diff { min-height: 240px; }

  .modal { max-width: 96vw; }
  .git-box { max-height: 140px; }
}

@media (max-width: 900px) and (min-width: 769px) {
  .studio-app { grid-template-rows: 48px 1fr 180px; }
  .workspace { grid-template-columns: 1fr; grid-template-rows: auto 1fr auto; }
  .sidebar { max-height: 120px; }
  .agent-pane { max-height: 240px; }
}

.auth-area { display: flex; align-items: center; gap: 6px; }
.auth-user { font-size: 0.78rem; color: #b7e4c7; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.modal {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  padding: 20px;
  max-width: 420px;
  width: 92vw;
}
.modal.wide { max-width: 560px; }
.modal::backdrop { background: rgba(0,0,0,0.6); }
.modal h3 { margin: 0 0 12px; }
.modal label { display: block; font-size: 0.8rem; color: var(--muted); margin: 8px 0 4px; }
.modal input, .modal textarea {
  width: 100%;
  background: #0d1117;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font-family: inherit;
}
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.rules-list { list-style: none; padding: 0; margin: 0 0 12px; max-height: 200px; overflow: auto; }
.rules-list li { padding: 8px; border-bottom: 1px solid var(--border); font-size: 0.82rem; }
.rules-list small { color: var(--muted); display: block; }
.sidebar-head small { color: var(--muted); font-weight: normal; }
.agent-head code { font-size: 0.75rem; color: #79c0ff; }
.mode-toggle { display: flex; gap: 4px; margin-bottom: 6px; }
.mode-toggle .mode { padding: 3px 10px; font-size: 0.72rem; }
.mode-toggle .mode.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.agent-head { display: flex; flex-direction: column; gap: 4px; }

.composer-modal { max-width: min(960px, 96vw); width: 96vw; padding: 0; border: none; }
.composer-wrap { display: grid; grid-template-columns: 200px 1fr; grid-template-rows: auto 1fr auto; gap: 0; min-height: 70vh; }
.composer-header { grid-column: 1 / -1; display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.composer-header h3 { margin: 0; font-size: 1rem; }
.composer-files { border-inline-end: 1px solid var(--border); overflow: auto; padding: 8px; background: #0d1117; }
.composer-files button { display: block; width: 100%; text-align: start; margin: 4px 0; padding: 8px; border-radius: 8px; border: 1px solid var(--border); background: var(--panel); color: var(--text); cursor: pointer; font-size: 0.78rem; }
.composer-files button.active { border-color: var(--primary); background: rgba(64,145,108,0.15); }
.composer-diff { min-height: 360px; height: 100%; }
.composer-actions { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; padding: 12px 16px; border-top: 1px solid var(--border); }
.msg.queue { background: rgba(255,193,7,0.12); color: #ffd866; font-size: 0.8rem; }

.mention-menu {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 100%;
  margin-bottom: 4px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  max-height: 160px;
  overflow: auto;
  z-index: 20;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
}
.mention-menu.hidden { display: none; }
.mention-menu button {
  display: block;
  width: 100%;
  text-align: start;
  padding: 8px 10px;
  border: none;
  background: none;
  color: var(--text);
  cursor: pointer;
  font-size: 0.8rem;
}
.mention-menu button:hover { background: rgba(64,145,108,0.15); }
.chat-form { position: relative; }

.git-box {
  background: #0d1117;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  max-height: 200px;
  overflow: auto;
  font-size: 0.78rem;
  white-space: pre-wrap;
}
.muted { color: var(--muted); font-size: 0.82rem; margin: 0 0 8px; }
