:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e3e8f0;
  --text: #1c2433;
  --muted: #667085;
  --primary: #4f46e5;
  --primary-strong: #4338ca;
  --primary-soft: #eef2ff;
  --green: #15803d;
  --green-soft: #dcfce7;
  --amber: #b45309;
  --amber-soft: #fef3c7;
  --red: #b91c1c;
  --red-soft: #fee2e2;
  --bubble-out: #4f46e5;
  --bubble-in: #ffffff;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 16px rgba(16, 24, 40, 0.06);
  --font: "Segoe UI", "Rubik", "Assistant", "Arial Hebrew", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1420;
    --surface: #171d2b;
    --surface-2: #1d2434;
    --border: #2a3346;
    --text: #e6e9f0;
    --muted: #98a2b3;
    --primary: #818cf8;
    --primary-strong: #a5b4fc;
    --primary-soft: #252a4a;
    --green: #4ade80;
    --green-soft: #12301f;
    --amber: #fbbf24;
    --amber-soft: #33270c;
    --red: #f87171;
    --red-soft: #3a1717;
    --bubble-out: #4f46e5;
    --bubble-in: #1d2434;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

[hidden] { display: none !important; }

a { color: var(--primary); }

h1, h2, h3 { margin: 0 0 0.5em; line-height: 1.3; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.1rem; }
.brand-logo { font-size: 1.3rem; }

.nav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.nav a {
  text-decoration: none;
  color: var(--muted);
  padding: 6px 12px;
  border-radius: 999px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--primary-soft); color: var(--primary); }

.badge-count {
  background: var(--red);
  color: #fff;
  border-radius: 999px;
  font-size: 0.75rem;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.user-box { display: flex; align-items: center; gap: 8px; color: var(--muted); }

/* ---------- layout ---------- */

.app {
  flex: 1;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px 16px;
  min-height: 0;
}

.app.chat-mode { display: flex; flex-direction: column; max-width: 860px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.card + .card { margin-top: 16px; }

.center-message { text-align: center; color: var(--muted); padding: 40px 0; }

.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

/* ---------- forms ---------- */

label { display: block; font-weight: 600; margin-bottom: 4px; font-size: 0.9rem; }

input[type="text"], input[type="password"], input[type="url"], input[type="search"],
input[type="date"], select, textarea {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 12px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
input[type="url"], input.ltr { direction: ltr; text-align: left; }
textarea { resize: vertical; }

.field { margin-bottom: 14px; }
.hint { color: var(--muted); font-size: 0.82rem; margin-top: 4px; }
.checkbox { display: flex; align-items: center; gap: 6px; font-weight: 400; }

.btn {
  font: inherit;
  font-weight: 600;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 9px 16px;
  cursor: pointer;
  background: var(--primary);
  color: #fff;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
.btn:hover { background: var(--primary-strong); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--surface-2); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { background: var(--red); }
.btn-success { background: var(--green); }
.btn-sm { padding: 5px 10px; font-size: 0.85rem; }

.alert { border-radius: 10px; padding: 10px 14px; margin-bottom: 12px; }
.alert-error { background: var(--red-soft); color: var(--red); }
.alert-ok { background: var(--green-soft); color: var(--green); }
.alert-info { background: var(--primary-soft); color: var(--primary); }
.alert-warn { background: var(--amber-soft); color: var(--amber); }

code, pre { font-family: Consolas, "Courier New", monospace; direction: ltr; text-align: left; }
.code-line { display: block; background: var(--surface-2); border: 1px solid var(--border); padding: 8px 10px; border-radius: 8px; margin-top: 6px; overflow-x: auto; }

/* ---------- login ---------- */

.login-wrap { max-width: 400px; margin: 8vh auto 0; }
.login-wrap h1 { text-align: center; }
.login-wrap .sub { text-align: center; color: var(--muted); margin-bottom: 20px; }

/* ---------- chat ---------- */

.chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-height: 0;
  height: calc(100vh - 110px);
}

.chat-head { padding: 14px 16px; border-bottom: 1px solid var(--border); display: grid; gap: 10px; }
.chat-head .customer { display: flex; gap: 8px; align-items: center; }
.chat-head .customer label { margin: 0; white-space: nowrap; }
.quick { display: flex; gap: 6px; flex-wrap: wrap; }
.quick .btn { border-radius: 999px; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--surface-2);
}

.msg { max-width: 80%; display: flex; flex-direction: column; gap: 4px; }
.msg.out { align-self: flex-start; align-items: flex-start; } /* RTL: start = right */
.msg.in { align-self: flex-end; align-items: flex-end; }

.bubble {
  padding: 10px 14px;
  border-radius: 16px;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.out .bubble { background: var(--bubble-out); color: #fff; border-end-start-radius: 4px; }
.msg.in .bubble { background: var(--bubble-in); border: 1px solid var(--border); border-end-end-radius: 4px; }
.msg .meta { font-size: 0.75rem; color: var(--muted); }
.msg .who { font-weight: 600; }

.typing { display: inline-flex; gap: 4px; align-items: center; }
.typing span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--muted);
  animation: blink 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

.composer { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--border); align-items: flex-end; }
.composer textarea { min-height: 46px; max-height: 180px; resize: none; }
.composer .hint { margin: 0; }

/* ---------- triage card ---------- */

.triage {
  background: var(--surface);
  border: 1px solid var(--border);
  border-inline-start: 4px solid var(--muted);
  border-radius: 12px;
  padding: 12px 14px;
  min-width: 280px;
  max-width: 520px;
}
.triage.u-low { border-inline-start-color: var(--green); }
.triage.u-medium { border-inline-start-color: var(--amber); }
.triage.u-high { border-inline-start-color: var(--red); }
.triage .badges { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.triage .summary { margin: 0 0 8px; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.78rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 2px 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
}
.badge.cat { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.badge.u-low { background: var(--green-soft); color: var(--green); border-color: transparent; }
.badge.u-medium { background: var(--amber-soft); color: var(--amber); border-color: transparent; }
.badge.u-high { background: var(--red-soft); color: var(--red); border-color: transparent; }
.badge.order { direction: ltr; }
.badge.resolved { background: var(--green-soft); color: var(--green); border-color: transparent; }

.route { font-weight: 700; display: flex; align-items: center; gap: 6px; }
.route.handled { color: var(--green); }
.route.waiting { color: var(--amber); }

details.tech { margin-top: 8px; font-size: 0.85rem; }
details.tech summary { cursor: pointer; color: var(--muted); }
details.tech pre {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  max-height: 280px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 6px 0 0;
}

.error-box { background: var(--red-soft); color: var(--red); border-radius: 12px; padding: 10px 14px; display: grid; gap: 8px; justify-items: start; }
.hint-box { background: var(--amber-soft); color: var(--amber); border-radius: 12px; padding: 10px 14px; }

/* ---------- inbox ---------- */

.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 14px; }
.tabs button {
  font: inherit; font-weight: 600;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  border-radius: 999px;
  padding: 6px 14px;
  cursor: pointer;
}
.tabs button.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 14px; }
.filters label { font-size: 0.8rem; color: var(--muted); }

.list { display: grid; gap: 10px; }
.item {
  display: grid;
  gap: 6px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-inline-start: 4px solid var(--muted);
  border-radius: 12px;
  text-decoration: none;
  color: inherit;
  cursor: pointer;
}
.item:hover { border-color: var(--primary); }
.item.u-low { border-inline-start-color: var(--green); }
.item.u-medium { border-inline-start-color: var(--amber); }
.item.u-high { border-inline-start-color: var(--red); }
.item .top { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.item .customer-name { font-weight: 700; }
.item .snippet { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.detail-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 16px; }
@media (max-width: 800px) { .detail-grid { grid-template-columns: 1fr; } }
.message-text { white-space: pre-wrap; word-break: break-word; background: var(--surface-2); border-radius: 10px; padding: 12px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

.notes { display: grid; gap: 8px; margin-bottom: 10px; }
.note { background: var(--surface-2); border-radius: 10px; padding: 8px 12px; }
.note .meta { font-size: 0.78rem; color: var(--muted); }
.note .text { white-space: pre-wrap; word-break: break-word; }

/* ---------- settings ---------- */

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: start; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { color: var(--muted); font-size: 0.85rem; font-weight: 600; }
td .row { gap: 6px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 640px) { .grid-2 { grid-template-columns: 1fr; } }

dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  background: var(--surface);
  color: var(--text);
  width: min(420px, 92vw);
}
dialog::backdrop { background: rgba(15, 20, 32, 0.45); }

/* ---------- toasts ---------- */

.toasts { position: fixed; bottom: 16px; inset-inline-start: 16px; display: grid; gap: 8px; z-index: 50; }
.toast {
  background: var(--text);
  color: var(--surface);
  padding: 10px 14px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  max-width: 360px;
}
.toast.error { background: var(--red); color: #fff; }

@media (max-width: 640px) {
  .topbar { padding: 8px 12px; gap: 8px; justify-content: space-between; }
  .nav { order: 3; flex: 1 0 100%; flex-wrap: nowrap; overflow-x: auto; }
  .nav a { white-space: nowrap; padding: 5px 10px; }
  .user-name { display: none; }
  .app { padding: 12px 8px; }
  .msg { max-width: 92%; }
  .chat { height: calc(100vh - 150px); }
}
