:root {
  --ground: #F6F8FA;
  --surface: #FFFFFF;
  --surface-subtle: #EEF2F5;
  --border: #DCE2E8;
  --border-strong: #CBD5E1;
  --ink: #0F172A;
  --ink-soft: #334155;
  --muted: #475569;
  --brand: #0E3B3D;
  --brand-hover: #134E50;
  --brand-tint: #E6F2F2;
  --brand-border: #B3D4D4;
  --accent: #0F766E;
  --ok: #0E7C66;
  --ok-bg: #ECFDF5;
  --ok-border: #A7F3D0;
  --warn: #B45309;
  --warn-bg: #FFFBEB;
  --warn-border: #FDE68A;
  --bad: #B91C1C;
  --bad-bg: #FEF2F2;
  --bad-border: #FECACA;
  --radius-card: 14px;
  --radius-ctrl: 10px;
  --shadow-overlay: 0 14px 36px rgba(15, 23, 42, 0.16);
}

*, *::before, *::after {
  box-sizing: border-box;
}

::selection {
  background: var(--brand);
  color: #FFFFFF;
}

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--ground);
  color: var(--ink);
  font-family: 'Segoe UI Arabic', 'Segoe UI', -apple-system, BlinkMacSystemFont, 'Noto Sans Arabic', system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
  caret-color: var(--brand);
}

a {
  color: var(--accent);
  text-underline-offset: 3px;
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.num, .stat b, td.num {
  font-variant-numeric: tabular-nums;
}

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Header & Navigation */
header.top {
  background: var(--brand);
  color: #FFFFFF;
  position: sticky;
  top: 0;
  z-index: 20;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}

.top-inner {
  max-width: 1040px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.top-title {
  flex: 1;
  min-width: 0;
}

header.top h1 {
  margin: 0;
  font-size: 1.0625rem;
  font-weight: 700;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

header.top .sub {
  font-size: 0.75rem;
  color: #CFE6E6;
  margin-top: 2px;
}

.desk-nav {
  display: none;
  align-items: center;
  gap: 4px;
}

.desk-nav a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: 9px;
  color: #D7EBEB;
  text-decoration: none;
  font-size: 0.875rem;
  font-weight: 600;
  transition: background-color 150ms ease, color 150ms ease;
}

.desk-nav a:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #FFFFFF;
}

.desk-nav a.on {
  background: #FFFFFF;
  color: var(--brand);
}

.nav-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 99px;
  font-size: 0.75rem;
  font-weight: 700;
  background: var(--warn);
  color: #FFFFFF;
}

.btn-header {
  min-height: 36px;
  padding: 0 12px;
  border-radius: 9px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.08);
  color: #FFFFFF;
  font-size: 0.8125rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  transition: background-color 150ms ease;
}

.btn-header:hover {
  background: rgba(255, 255, 255, 0.18);
}

/* Main Shell Container */
main.workspace {
  width: 100%;
  max-width: 1040px;
  margin: 0 auto;
  flex: 1;
  padding: 16px 16px 96px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

main.workspace.chat-mode {
  padding-bottom: 144px;
}

/* Mobile Bottom Tabs */
nav.tabs {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
  z-index: 20;
}

nav.tabs a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 6px 2px;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.75rem;
  font-weight: 600;
  border-radius: 8px;
  position: relative;
}

nav.tabs a.on {
  color: var(--brand);
}

nav.tabs a .nav-badge {
  position: absolute;
  top: 2px;
  inset-inline-end: 18%;
}

/* Cards & Summary Strips */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.card-title {
  margin: 0;
  font-size: 0.9375rem;
  font-weight: 700;
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.stat-strip {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  overflow: hidden;
}

.stat-strip.cols-3 {
  grid-template-columns: repeat(1, minmax(0, 1fr));
}

.stat-cell {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  border-inline-end: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-cell.highlight {
  background: var(--brand-tint);
}

.stat-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
}

.stat-val {
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.stat-cell.highlight .stat-val {
  color: var(--brand);
}

/* Utilities & Grids */
.row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.between {
  justify-content: space-between;
}

.wrap {
  flex-wrap: wrap;
}

.grow {
  flex: 1;
  min-width: 0;
}

.grid2 {
  display: grid;
  grid-template-columns: repeat(1, minmax(0, 1fr));
  gap: 10px;
}

.detail-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 14px;
  align-items: start;
}

.detail-col {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.items-list {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 10px;
}

.muted {
  color: var(--muted);
  font-size: 0.8125rem;
}

/* Badges & Alerts */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 8px;
  border: 1px solid transparent;
  white-space: nowrap;
}

.badge-ok { background: var(--ok-bg); color: var(--ok); border-color: var(--ok-border); }
.badge-warn { background: var(--warn-bg); color: var(--warn); border-color: var(--warn-border); }
.badge-bad { background: var(--bad-bg); color: var(--bad); border-color: var(--bad-border); }
.badge-info { background: var(--brand-tint); color: var(--brand); border-color: var(--brand-border); }
.badge-neutral { background: var(--surface-subtle); color: var(--ink-soft); border-color: var(--border); }

.banner {
  border-radius: var(--radius-card);
  padding: 12px 14px;
  border: 1px solid var(--border);
  display: flex;
  align-items: flex-start;
  gap: 10px;
  font-size: 0.875rem;
}

.banner.alert { background: var(--bad-bg); border-color: var(--bad-border); color: #7F1D1D; }
.banner.notice { background: var(--warn-bg); border-color: var(--warn-border); color: #78350F; }
.banner.good { background: var(--ok-bg); border-color: var(--ok-border); color: #065F46; }

/* Buttons & Form Controls */
.btn {
  min-height: 42px;
  padding: 0 14px;
  border-radius: var(--radius-ctrl);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--ink);
  font-size: 0.875rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  transition: background-color 150ms ease, border-color 150ms ease, transform 100ms ease;
}

.btn:hover:not(:disabled) {
  background: var(--surface-subtle);
}

.btn:active:not(:disabled) {
  transform: scale(0.985);
}

.btn.primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #FFFFFF;
}

.btn.primary:hover:not(:disabled) {
  background: var(--brand-hover);
  border-color: var(--brand-hover);
}

.btn.danger {
  background: var(--surface);
  border-color: var(--bad-border);
  color: var(--bad);
}

.btn.danger:hover:not(:disabled) {
  background: var(--bad-bg);
}

.btn.small {
  min-height: 36px;
  padding: 0 10px;
  font-size: 0.8125rem;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

label.field {
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--ink-soft);
  display: flex;
  flex-direction: column;
  gap: 5px;
}

input, select, textarea {
  min-height: 42px;
  border-radius: var(--radius-ctrl);
  border: 1px solid var(--border-strong);
  padding: 0 12px;
  background: var(--surface);
  width: 100%;
  font-size: 0.9375rem;
  transition: border-color 150ms ease, background-color 150ms ease;
}

input:hover:not(:disabled), select:hover:not(:disabled), textarea:hover:not(:disabled) {
  border-color: #94A3B8;
}

input:disabled, select:disabled, textarea:disabled {
  background: var(--surface-subtle);
  color: var(--muted);
  cursor: not-allowed;
}

textarea {
  padding: 10px 12px;
  min-height: 74px;
  resize: vertical;
}

.input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.input-wrap input {
  padding-inline-end: 42px;
}

.input-addon-btn {
  position: absolute;
  inset-inline-end: 6px;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--muted);
  border-radius: 7px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.input-addon-btn:hover {
  background: var(--surface-subtle);
  color: var(--ink);
}

.err-text {
  color: var(--bad);
  font-size: 0.8125rem;
  font-weight: 600;
}

.dirty-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  border-radius: var(--radius-ctrl);
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  font-size: 0.8125rem;
  font-weight: 600;
  color: #78350F;
}

/* Filter Chips */
.chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 4px;
}

.chip {
  min-height: 36px;
  border-radius: 99px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--ink-soft);
  font-size: 0.8125rem;
  font-weight: 600;
  padding: 0 14px;
  white-space: nowrap;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background-color 150ms ease, color 150ms ease, border-color 150ms ease;
}

.chip:hover {
  background: var(--surface-subtle);
}

.chip.on {
  background: var(--brand);
  border-color: var(--brand);
  color: #FFFFFF;
}

/* Item Cards & Thumbnails */
.item-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  text-decoration: none;
  color: inherit;
  transition: border-color 150ms ease, background-color 150ms ease;
}

.item-card:hover {
  border-color: var(--brand-border);
  background: #FAFCFC;
}

.thumb {
  width: 56px;
  height: 56px;
  border-radius: 10px;
  object-fit: cover;
  background: var(--surface-subtle);
  border: 1px solid var(--border);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--muted);
}

/* Login Centered Surface */
.login-shell {
  width: 100%;
  max-width: 440px;
  margin: 28px auto;
  padding: 0 16px;
}

/* Chat Surface */
.chat {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg {
  max-width: 82%;
  padding: 10px 13px;
  border-radius: 14px;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-size: 0.9375rem;
  border: 1px solid var(--border);
}

.msg.me {
  align-self: flex-start;
  background: var(--brand-tint);
  border-color: var(--brand-border);
}

[dir=ltr] .msg.me {
  align-self: flex-end;
}

.msg.them {
  align-self: flex-end;
  background: var(--surface);
}

[dir=ltr] .msg.them {
  align-self: flex-start;
}

.msg.system {
  align-self: stretch;
  max-width: 100%;
  background: var(--warn-bg);
  border-color: var(--warn-border);
  font-size: 0.875rem;
  color: #78350F;
}

.msg img {
  max-width: 240px;
  border-radius: 10px;
  display: block;
  margin-bottom: 6px;
  cursor: zoom-in;
  border: 1px solid var(--border);
}

.msg .meta {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 5px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.composer-wrap {
  position: fixed;
  bottom: calc(56px + env(safe-area-inset-bottom));
  left: 0;
  right: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 15;
}

.composer {
  max-width: 1040px;
  margin: 0 auto;
  padding: 8px 14px;
  display: flex;
  gap: 8px;
  align-items: center;
}

/* Timeline Stepper */
.steps {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.step-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.875rem;
}

.step-row.done {
  color: var(--ink);
}

.step-row.now {
  font-weight: 700;
  color: var(--brand);
}

.dot {
  width: 12px;
  height: 12px;
  border-radius: 99px;
  background: var(--border-strong);
  flex-shrink: 0;
}

.dot.done {
  background: var(--ok);
}

.dot.now {
  background: var(--warn);
  box-shadow: 0 0 0 3px var(--warn-bg);
}

/* Proofs & Media */
.proof-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
  gap: 8px;
}

.proof-tile {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px;
  background: var(--ground);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  text-align: center;
  font-size: 0.75rem;
  color: var(--ink-soft);
}

.proofimg {
  width: 100%;
  height: 70px;
  object-fit: cover;
  border-radius: 7px;
  cursor: zoom-in;
  display: block;
}

/* Modals, Lightbox & Toast */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 40;
  padding: 0;
}

.sheet {
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  padding: 18px;
  width: 100%;
  max-width: 540px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 90vh;
  overflow: auto;
  box-shadow: var(--shadow-overlay);
}

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.88);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 20px;
  z-index: 50;
}

.lightbox img {
  max-width: 95vw;
  max-height: 82vh;
  border-radius: 12px;
  background: #FFFFFF;
}

.toast-root {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  width: min(92vw, 420px);
}

.toast {
  background: var(--ink);
  color: #FFFFFF;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 0.875rem;
  font-weight: 600;
  box-shadow: var(--shadow-overlay);
  display: flex;
  align-items: center;
  gap: 8px;
}

.toast.ok { background: var(--ok); }
.toast.err { background: var(--bad); }

details.audit-box summary {
  cursor: pointer;
  font-weight: 700;
  font-size: 0.875rem;
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

details.audit-box summary::-webkit-details-marker {
  display: none;
}

.empty-state {
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-card);
  padding: 28px 18px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  color: var(--muted);
}

.kv-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 0;
  border-bottom: 1px solid var(--surface-subtle);
  font-size: 0.875rem;
}

.kv-row:last-child {
  border-bottom: none;
}

@media (min-width: 600px) {
  .grid2 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .stat-strip.cols-3 {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  .stat-strip.cols-4 {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
  .modal {
    align-items: center;
    padding: 16px;
  }
  .sheet {
    border-radius: 16px;
  }
}

@media (min-width: 768px) {
  nav.tabs {
    display: none;
  }
  .desk-nav {
    display: flex;
  }
  .composer-wrap {
    bottom: 0;
  }
  main.workspace {
    padding: 20px 24px 40px;
  }
  main.workspace.chat-mode {
    padding-bottom: 86px;
  }
  .items-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (min-width: 900px) {
  .detail-grid {
    grid-template-columns: minmax(0, 1.25fr) minmax(0, 0.95fr);
  }
}
