/* SAUDSD AI dashboard — dark, RTL, no external fonts/CDNs */
* { box-sizing: border-box; }

:root {
  --bg: #090e17;
  --panel: #111a2b;
  --panel2: #16223a;
  --line: #223350;
  --fg: #e8eefc;
  --muted: #8ba0c4;
  --accent: #2f81f7;
  --err: #ff6b6b;
}

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Tahoma, Arial, sans-serif;
}

.hide { display: none !important; }

input, textarea, button {
  font: inherit;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel2);
  color: var(--fg);
  padding: 10px 12px;
  outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); }
button {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  cursor: pointer;
  font-weight: 600;
}
button:hover { filter: brightness(1.08); }
textarea { width: 100%; resize: vertical; }

.err { color: var(--err); min-height: 18px; margin-top: 8px; font-size: 13px; }

/* ---------- login ---------- */
.login {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login .card { width: 100%; max-width: 360px; text-align: center; }
.login input { width: 100%; margin: 10px 0; }
.login button { width: 100%; }

.brand { font-size: 22px; font-weight: 800; letter-spacing: .5px; }
.sub { color: var(--muted); font-size: 13px; }
.title { font-size: 22px; font-weight: 700; margin: 4px 0 0; }

/* ---------- shell ---------- */
.shell {
  display: grid;
  grid-template-columns: 240px 1fr;
  min-height: 100%;
}
.side {
  background: var(--panel);
  border-inline-start: 1px solid var(--line);
  padding: 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.nav { display: flex; flex-direction: column; gap: 6px; }
.nav a, .bottom a {
  display: block;
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--fg);
  text-decoration: none;
  cursor: pointer;
  border: 1px solid transparent;
}
.nav a:hover { background: var(--panel2); }
.nav a.active { background: var(--panel2); border-color: var(--accent); font-weight: 700; }
.bottom { margin-top: auto; }

.main { padding: 20px 24px; }
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}
.toolbar { display: flex; gap: 10px; align-items: center; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
  margin-bottom: 14px;
}
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px;
}
.metric .title { font-size: 26px; }
.card .row {
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.card .row.active-row { border-inline-start: 3px solid var(--accent); padding-inline-start: 8px; }
.card .row:last-child { border-bottom: 0; }

.section-label { margin-top: 14px; font-weight: 600; }
.hint { margin-bottom: 10px; }

.action-card, .file-card {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 10px;
}
.action-head, .file-head { font-weight: 700; margin-bottom: 4px; }
.action-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
  align-items: center;
}
.action-controls select,
.action-controls input[type="date"] {
  min-width: 120px;
}
.btn-link {
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
}
button.danger {
  background: #c0392b;
  border-color: #c0392b;
}
.edge-row { font-size: 13px; }

.graph {
  min-height: 220px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  margin-top: 10px;
  padding: 10px;
  overflow: auto;
}

pre {
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  margin-top: 10px;
  min-height: 40px;
}

/* ---------- pages ---------- */
.page { display: none; }
.page.active { display: block; }

/* Route guards: keep /chat from ever showing the dashboard shell */
html[data-route="chat"] #app,
html[data-route="chat"] #chooser {
  display: none !important;
}

html[data-route="app"] #chatView,
html[data-route="app"] #chooser {
  display: none !important;
}

body.mode-chat #app,
body.mode-chat #chooser {
  display: none !important;
}

body.mode-app #chatView,
body.mode-app #chooser {
  display: none !important;
}

html[data-route="chat"] #chatView:not(.hide) {
  display: flex !important;
}

.mobile-nav {
  display: none;
}

@media (max-width: 768px) {
  .shell {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
    grid-template-columns: 1fr;
  }

  .side {
    display: none !important;
  }

  .main {
    flex: 1;
    overflow: auto;
    padding: 14px 14px calc(92px + env(safe-area-inset-bottom, 0px));
  }

  .top {
    position: sticky;
    top: 0;
    z-index: 2;
    background: var(--bg);
    padding-bottom: 8px;
    margin-bottom: 12px;
  }

  .mobile-nav {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    gap: 4px;
    padding: 8px 6px calc(8px + env(safe-area-inset-bottom, 0px));
    background: var(--panel);
    border-top: 1px solid var(--line);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .mobile-nav a {
    flex: 1 0 auto;
    min-width: 58px;
    text-align: center;
    text-decoration: none;
    color: var(--fg);
    font-size: 11px;
    line-height: 1.2;
    padding: 8px 6px;
    border-radius: 10px;
    border: 1px solid transparent;
    white-space: nowrap;
  }

  .mobile-nav a.active {
    background: var(--panel2);
    border-color: var(--accent);
    font-weight: 700;
  }

  html[data-route="chat"] .mobile-nav,
  body.mode-chat .mobile-nav {
    display: none !important;
  }
}

@media (max-width: 1023px) {
  .chat-menu-btn {
    display: inline-flex;
  }

  .chat-sidebar {
    position: fixed;
    top: 57px;
    bottom: 0;
    inset-inline-end: 0;
    z-index: 30;
    transform: translateX(0);
    transition: transform .2s ease;
    box-shadow: -8px 0 24px rgba(0, 0, 0, .35);
  }

  html[dir="rtl"] .chat-sidebar {
    box-shadow: 8px 0 24px rgba(0, 0, 0, .35);
  }

  .chat-sidebar.collapsed {
    transform: translateX(100%);
  }

  html[dir="rtl"] .chat-sidebar.collapsed {
    transform: translateX(-100%);
  }
}

@media (min-width: 1024px) {
  .chat-menu-btn {
    display: none !important;
  }

  .chat-sidebar {
    position: relative;
    transform: none !important;
    box-shadow: none;
  }

  .chat-sidebar.collapsed {
    transform: none !important;
  }
}

@media (max-width: 720px) {
  .chooser-actions { grid-template-columns: 1fr; }
  .chat-input-row {
    grid-template-columns: auto 1fr;
  }
  .chat-input-row button:last-child {
    grid-column: 1 / -1;
    width: 100%;
  }
  .chat-input-bar button { width: 100%; }
}

.blind-panel textarea { width: 100%; margin-top: 6px; }
.blind-grid { display: grid; grid-template-columns: 160px 1fr; gap: 12px; align-items: start; }
.blind-pre {
  white-space: pre-wrap;
  background: #0b1220;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  max-height: 320px;
  overflow: auto;
  direction: ltr;
  text-align: left;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

/* ---------- chooser ---------- */
.chooser-card { max-width: 420px; }
.chooser-actions {
  display: grid;
  gap: 12px;
  margin-top: 18px;
}
.chooser-btn {
  width: 100%;
  padding: 16px 14px;
  font-size: 18px;
}
.chooser-btn-alt {
  background: var(--panel2);
  border-color: var(--line);
  color: var(--fg);
}

/* ---------- full-screen chat (/chat) ---------- */
.chat-shell {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.view-header-start {
  display: flex;
  align-items: center;
  gap: 8px;
}
.chat-menu-btn {
  display: none;
}
.chat-body {
  flex: 1;
  min-height: 0;
  display: flex;
}
.chat-sidebar {
  width: 280px;
  flex: 0 0 280px;
  border-inline-start: 1px solid var(--line);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.chat-new-btn {
  margin: 12px;
  width: calc(100% - 24px);
  text-align: start;
  background: var(--panel2);
  border-color: var(--line);
  color: var(--fg);
}
.chat-session-groups {
  flex: 1;
  overflow: auto;
  padding: 0 8px 12px;
}
.chat-session-group {
  margin-top: 10px;
}
.chat-session-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  padding: 6px 8px;
}
.chat-session-item {
  display: block;
  width: 100%;
  text-align: start;
  padding: 10px 12px;
  margin-bottom: 4px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--fg);
  cursor: pointer;
  font: inherit;
  line-height: 1.35;
}
.chat-session-item:hover {
  background: var(--panel2);
}
.chat-session-item.active {
  background: var(--panel2);
  border-color: var(--accent);
  font-weight: 700;
}
.chat-pane {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.compact-brand { font-size: 18px; }
.view-header-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.header-link {
  color: var(--accent);
  text-decoration: none;
  font-weight: 700;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel2);
}
.header-btn {
  padding: 8px 12px;
  font-size: 14px;
}
.chat-main {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: 0;
  background: var(--bg);
}
.chat-main.chat-drop-active {
  outline: 2px dashed var(--accent);
  outline-offset: -6px;
  background: color-mix(in srgb, var(--accent) 8%, var(--bg));
}
.chat-thread-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  padding: 14px 10px;
  padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px));
}
.chat-thread {
  width: 100%;
  max-width: 70ch;
  margin-inline: auto;
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.chat-bubble {
  max-width: 100%;
  padding: 10px 12px;
  margin-inline: 10px;
  border-radius: 14px;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.45;
  font-size: 14px;
  position: relative;
}
.chat-bubble.user {
  align-self: start;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-bubble.assistant {
  align-self: end;
  background: var(--panel);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
}
.chat-bubble.system {
  align-self: center;
  margin-inline: auto;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  padding: 4px 8px;
}
.chat-bubble img {
  display: block;
  max-width: min(100%, 280px);
  border-radius: 10px;
  margin-bottom: 8px;
}
.chat-bubble-actions {
  display: flex;
  justify-content: end;
  margin-bottom: 6px;
}
.chat-copy-btn {
  padding: 4px 8px;
  font-size: 11px;
  min-height: 0;
  height: auto;
  background: var(--panel2);
  border-color: var(--line);
  color: var(--fg);
}
.chat-bubble.user .chat-copy-btn {
  background: rgba(255, 255, 255, 0.15);
  border-color: rgba(255, 255, 255, 0.25);
  color: #fff;
}
.chat-bubble-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.chat-code-block {
  position: relative;
  background: #0b1220;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}
.chat-code-block pre {
  margin: 0;
  padding: 10px 12px;
  background: transparent;
  border: 0;
  border-radius: 0;
  min-height: 0;
  overflow-x: auto;
  direction: ltr;
  text-align: left;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}
.chat-code-block .chat-copy-btn {
  position: absolute;
  top: 6px;
  inset-inline-end: 6px;
}
.chat-typing-indicator {
  align-self: end;
  margin-inline: 10px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 10px 14px;
  border-radius: 14px;
  border-bottom-left-radius: 4px;
  background: var(--panel);
  border: 1px solid var(--line);
}
.chat-typing-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: chat-typing-bounce 1.2s infinite ease-in-out;
}
.chat-typing-dot:nth-child(2) { animation-delay: .15s; }
.chat-typing-dot:nth-child(3) { animation-delay: .3s; }
@keyframes chat-typing-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: .45; }
  40% { transform: translateY(-4px); opacity: 1; }
}
.chat-output {
  margin: 0;
  min-height: calc(100% - 8px);
  background: transparent;
  border: 0;
  padding: 0;
}
.chat-input-bar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--line);
  background: var(--panel);
}
.chat-input-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: end;
}
.chat-icon-btn {
  min-width: 44px;
  height: 44px;
  padding: 0;
  font-size: 18px;
  background: var(--panel2);
  border-color: var(--line);
  color: var(--fg);
}
.chat-image-preview {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.chat-image-preview img {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 8px;
}
.chat-image-preview button {
  margin-inline-start: auto;
  background: #c0392b;
  border-color: #c0392b;
}
.project-toolbar {
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.project-toolbar input {
  min-width: 140px;
  flex: 1;
}
.project-edit {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}
.project-edit input,
.project-edit textarea {
  width: 100%;
}
.chat-input-bar textarea {
  margin: 0;
  min-height: 44px;
  max-height: 144px;
  overflow-y: auto;
  resize: none;
  line-height: 1.45;
}
.chat-input-bar button {
  min-width: 72px;
  height: 44px;
  padding-inline: 16px;
}
