/* Localhost.sa: one stylesheet for both directions.
 *
 * RTL rule of thumb: never write left/right. Use LOGICAL properties:
 *   margin-left   → margin-inline-start      text-align: left → text-align: start
 *   padding-right → padding-inline-end       left: 0          → inset-inline-start: 0
 * "start" means left in English and right in Arabic, so <html dir="rtl">
 * mirrors the whole layout with zero extra CSS.
 */

:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --text: #16202c;
  --muted: #5b6878;
  --border: #dde2e8;
  --accent: #0b7a5a;        /* green, a nod to the Saudi flag */
  --accent-hover: #09644a;
  --accent-text: #ffffff;
  --danger: #b42318;
  --danger-bg: #fef3f2;
  --danger-text: #ffffff;
  --warn-bg: #fffaeb;
  --warn-border: #f0c36d;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", "Noto Sans Arabic", "Noto Sans", Tahoma, sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141a;
    --surface: #171e26;
    --text: #e6ebf0;
    --muted: #9aa7b5;
    --border: #2a3440;
    --accent: #2fbf8a;
    --accent-hover: #4fd3a1;
    --accent-text: #08130e;
    --danger: #ff8a80;
    --danger-bg: #2a1614;
    --danger-text: #2a0e0b;
    --warn-bg: #261f0e;
    --warn-border: #7a5d1c;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 1rem;
  line-height: 1.6;
}

/* Arabic script needs a little more line height to stay readable. */
html[lang="ar"] body { line-height: 1.8; }

a { color: var(--accent); }
a:hover { color: var(--accent-hover); }

/* --- Top bar ---------------------------------------------------------------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding-block: 0.75rem;
  padding-inline: max(1rem, calc((100% - 960px) / 2));
  background: var(--surface);
  border-block-end: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.15rem; color: var(--text); text-decoration: none; }
.brand span { color: var(--accent); }
.nav { display: flex; align-items: center; flex-wrap: wrap; gap: 0.25rem 1rem; }
.nav a { text-decoration: none; }
.inline { display: inline; margin: 0; }

/* --- Layout ------------------------------------------------------------------ */
.container {
  flex: 1;
  width: 100%;
  max-width: 960px;
  margin-inline: auto;
  padding: 2rem 1rem;
}
.footer {
  padding: 1.5rem 1rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.875rem;
}

.hero { padding-block: 3rem; max-width: 40rem; }
.hero h1 { font-size: clamp(1.75rem, 4vw, 2.5rem); line-height: 1.25; margin-block: 0 1rem; }
.lead { font-size: 1.15rem; color: var(--muted); margin-block: 0 2rem; }
.hero .button { margin-block-start: 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}
.card.narrow { max-width: 26rem; margin-inline: auto; }
.card.empty { text-align: center; padding-block: 2.5rem; }
.card h1 { font-size: 1.5rem; margin-block: 0 1.25rem; }

/* --- Forms ------------------------------------------------------------------- */
label { display: block; font-weight: 600; margin-block: 1rem 0.35rem; }
input[type="email"], input[type="password"], input[type="text"] {
  width: 100%;
  padding: 0.6rem 0.75rem;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) - 4px);
}
input:focus-visible, button:focus-visible, a:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}
input[aria-invalid="true"] { border-color: var(--danger); }

.hint { font-size: 0.875rem; color: var(--muted); margin-block: 0.35rem 0; }
.field-error { font-size: 0.875rem; color: var(--danger); margin-block: 0.35rem 0; }
.alert {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid currentColor;
  border-radius: calc(var(--radius) - 4px);
  padding: 0.6rem 0.85rem;
}
.muted { color: var(--muted); }

/* --- Buttons ----------------------------------------------------------------- */
.button {
  display: inline-block;
  margin-block-start: 1.5rem;
  padding: 0.65rem 1.25rem;
  font: inherit;
  font-weight: 600;
  color: var(--accent-text);
  background: var(--accent);
  border: 0;
  border-radius: calc(var(--radius) - 4px);
  text-decoration: none;
  cursor: pointer;
}
.button:hover { background: var(--accent-hover); color: var(--accent-text); }
form .button { width: 100%; }
.button.small { margin: 0; padding: 0.35rem 0.85rem; font-size: 0.9rem; }
button.link {
  padding: 0;
  font: inherit;
  color: var(--accent);
  background: none;
  border: 0;
  cursor: pointer;
}
button.link:hover { text-decoration: underline; }

/* --- Dashboard ------------------------------------------------------------ */
h2 { font-size: 1.15rem; margin-block: 0 0.5rem; }
.card.create { margin-block-start: 1.5rem; max-width: 32rem; }
.card.wide { max-width: 44rem; margin-inline: auto; }

.table-wrap { overflow-x: auto; }   /* narrow screens: scroll the table, not the page */
table.dbs {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.dbs th, .dbs td {
  padding: 0.65rem 0.85rem;
  text-align: start;                /* right in Arabic, left in English */
  border-block-end: 1px solid var(--border);
  white-space: nowrap;
}
.dbs th { font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.dbs tbody tr:last-child td { border-block-end: 0; }
@media (max-width: 640px) { .col-created { display: none; } }

/* --- Admin page ------------------------------------------------------------- */
.page-head h1 { margin-block: 0 0.25rem; }
.page-head p { margin-block: 0; }

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
  gap: 1rem;
  margin-block: 1.5rem;
}
.stat {
  display: grid;
  align-content: start;
  gap: 0.15rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
}
.stat-label { color: var(--muted); font-size: 0.875rem; font-weight: 600; }
.stat-value { font-size: 2.1rem; line-height: 1.15; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-sub { color: var(--muted); font-size: 0.8rem; }

.admin-users td { vertical-align: top; }
.admin-users th:last-child, .admin-users td:last-child { white-space: normal; }
.user-email { display: flex; flex-wrap: wrap; align-items: center; gap: 0.4rem; font-weight: 600; }
.user-meta { display: flex; flex-wrap: wrap; gap: 0 0.75rem; color: var(--muted); font-size: 0.8rem; }
.user-id { font-variant-numeric: tabular-nums; }
.badge {
  display: inline-block;
  padding: 0 0.5rem;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1.6;
}

/* Usage: "3 / 10" over a bar of one segment per allowed database. */
.usage-cell { min-width: 7.5rem; }
.usage-count { display: block; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 0.875rem; }
.usage-count b { color: var(--text); font-size: 1rem; }
.usage-bar { display: flex; gap: 2px; margin-block-start: 0.3rem; }
.seg { flex: 1; height: 0.4rem; min-width: 0.4rem; border-radius: 2px; background: var(--border); }
.seg.used { background: var(--accent); }

/* Identifiers wrap as chips instead of running off the table. */
.ids-cell { min-width: 12rem; }
.chip {
  display: inline-block;
  margin: 0.1rem 0;
  margin-inline-end: 0.3rem;
  padding: 0.05rem 0.45rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  font-size: 0.8rem;
}

code, .mono { font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace; font-size: 0.9em; }

.status {
  display: inline-block;
  padding: 0.05rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
}
.status-ready { color: var(--accent); border-color: currentColor; }
.status-failed { color: var(--danger); border-color: currentColor; }

.danger-link { color: var(--danger); }
.danger-link:hover { color: var(--danger); }
.button.danger { background: var(--danger); color: var(--danger-text); }
.button.danger:hover { background: var(--danger); filter: brightness(0.92); }

.warning {
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  border-radius: calc(var(--radius) - 4px);
  padding: 0.6rem 0.85rem;
}

.copy-row { display: flex; gap: 0.5rem; }
.copy-row input { flex: 1; min-width: 0; }
.copy-row .button.small { flex: none; }

.sr-only {                           /* visible to screen readers only */
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip-path: inset(50%); white-space: nowrap;
}

/* --- htmx busy state ---------------------------------------------------------
 * While a request runs, htmx adds the class "htmx-request" to the form (and
 * disables the button via hx-disabled-elt), so we swap the button text.
 * We style this ourselves because htmx's built-in indicator CSS would need
 * an inline <style>, which our Content-Security-Policy forbids. */
.when-busy { display: none; }
.htmx-request .when-idle { display: none; }
.htmx-request .when-busy { display: inline; }
button[disabled] { opacity: 0.75; cursor: progress; }
