:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e3e6eb;
  --text: #1c2430;
  --muted: #6b7584;
  --accent: #0f7b6c;
  --accent-ink: #ffffff;
  --accent-soft: #e3f3f0;
  --danger: #c0392b;
  --danger-soft: #fbeaea;
  --warn: #a8670a;
  --warn-soft: #fdf2e0;
  --info: #2b5fb4;
  --info-soft: #e8effb;
  --ok: #1d7f45;
  --ok-soft: #e4f4ea;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161c;
    --surface: #1a2028;
    --surface-2: #202833;
    --border: #2d3743;
    --text: #e6e9ee;
    --muted: #9aa4b2;
    --accent: #35b39f;
    --accent-ink: #0b1512;
    --accent-soft: #173a35;
    --danger: #ef7a6c;
    --danger-soft: #3b1f1d;
    --warn: #f0b45a;
    --warn-soft: #3a2c15;
    --info: #7fa8f0;
    --info-soft: #1d2a40;
    --ok: #6fd39a;
    --ok-soft: #173325;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.6 "Vazirmatn", "Vazir", "IRANSans", Tahoma, system-ui, sans-serif;
}

[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.3; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1.2em; margin: 8px 0 0; }
.num { font-variant-numeric: tabular-nums; }
.ltr { direction: ltr; unicode-bidi: isolate; display: inline-block; }

/* ---------- controls ---------- */
input, select, textarea {
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  min-width: 0;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label > input, label > select, label > textarea { color: var(--text); }

.btn {
  font: inherit;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 7px 14px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.small { padding: 3px 10px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 16px; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px;
  display: flex; flex-direction: column; gap: 14px;
  box-shadow: var(--shadow);
}
.login-card h1 { font-size: 20px; }
.login-card .muted { margin: -8px 0 4px; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface);
  border-inline-end: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 16px 12px;
  position: sticky; top: 0; height: 100vh;
}
.brand { font-weight: 700; font-size: 16px; padding: 4px 10px 16px; }
nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
nav a {
  color: var(--text); text-decoration: none;
  padding: 8px 10px; border-radius: 8px;
  display: flex; justify-content: space-between; align-items: center;
}
nav a:hover { background: var(--surface-2); }
nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
nav .sep { height: 1px; background: var(--border); margin: 8px 10px; }
nav .count { font-size: 11px; background: var(--danger); color: #fff; border-radius: 99px; padding: 0 7px; }
.me { border-top: 1px solid var(--border); padding: 12px 10px 0; display: flex; flex-direction: column; gap: 4px; }
.me .btn { align-self: flex-start; margin-top: 6px; }

.content { padding: 24px 28px 48px; min-width: 0; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head h2 { font-size: 20px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---------- cards / stats ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 20px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
  text-decoration: none; color: inherit; display: block;
}
a.stat:hover { border-color: var(--accent); }
.stat .label { color: var(--muted); font-size: 12px; }
.stat .value { font-size: 24px; font-weight: 700; margin-top: 4px; }
.stat .value.sm { font-size: 17px; padding-top: 6px; }
.stat .hint { font-size: 12px; color: var(--muted); margin-top: 2px; }
.stat.alert { border-color: var(--danger); }
.stat.alert .value { color: var(--danger); }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden;
}
.card > h3 { font-size: 14px; padding: 12px 16px; border-bottom: 1px solid var(--border); }

/* ---------- filters ---------- */
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 14px; }
.filters label { min-width: 150px; }
.filters .grow { flex: 1; min-width: 200px; }
.date-hint { font-size: 12px; color: var(--muted); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: start; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; font-weight: 600; color: var(--muted); background: var(--surface-2); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
td .sub { display: block; font-size: 12px; color: var(--muted); }
td.actions-cell { white-space: nowrap; text-align: end; }
td.actions-cell .btn + .btn { margin-inline-start: 4px; }
.empty { padding: 32px; text-align: center; color: var(--muted); }
tr.inactive td { opacity: .55; }

.badge { display: inline-block; font-size: 12px; padding: 1px 9px; border-radius: 99px; white-space: nowrap; }
.badge.pending { background: var(--warn-soft); color: var(--warn); }
.badge.sent_to_trader { background: var(--info-soft); color: var(--info); }
.badge.issued, .badge.open { background: var(--ok-soft); color: var(--ok); }
.badge.rejected, .badge.off { background: var(--danger-soft); color: var(--danger); }
.badge.closed { background: var(--surface-2); color: var(--muted); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 14px; flex-wrap: wrap; }
.tabs button {
  font: inherit; background: none; border: 0; padding: 8px 14px; cursor: pointer; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

/* ---------- dialog ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 0;
  width: min(520px, calc(100vw - 32px));
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / 40%); }
dialog header { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
dialog header h3 { font-size: 16px; }
#dialog-body { padding: 16px 18px 0; display: grid; gap: 12px; max-height: 65vh; overflow: auto; }
#dialog-error { padding: 0 18px; }
dialog footer { display: flex; gap: 8px; justify-content: flex-start; padding: 12px 18px 16px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.kv { display: grid; grid-template-columns: 130px 1fr; gap: 6px 12px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.pre { white-space: pre-wrap; background: var(--surface-2); border-radius: 8px; padding: 10px; font-size: 13px; max-height: 200px; overflow: auto; }
.price-preview { font-size: 13px; background: var(--accent-soft); color: var(--accent); padding: 8px 12px; border-radius: 8px; }

/* ---------- toast ---------- */
#toast {
  position: fixed; bottom: 20px; inset-inline-start: 50%; transform: translateX(50%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px;
  opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 10; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; }
#toast.err { background: var(--danger); color: #fff; }

/* ---------- mobile ---------- */
@media (max-width: 760px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar { min-width: 0; position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px 16px; }
  .brand { padding: 0; }
  nav { flex-direction: row; overflow-x: auto; order: 3; flex-basis: 100%; min-width: 0; }
  nav .sep { display: none; }
  nav a { white-space: nowrap; }
  .me { border: 0; padding: 0; flex-direction: row; align-items: center; margin-inline-start: auto; }
  .me .btn { margin: 0; }
  #me-role { display: none; }
  .content { padding: 16px; }
  .row2 { grid-template-columns: 1fr; }
}
