@layer base, layout, components, forms, dashboard;

@layer base {
  :root {
    color-scheme: light;
    --ink: #171c1e;
    --muted: #3e484d;
    --line: #bdc8ce;
    --outline: #6e797e;
    --paper: #f6fafd;
    --surface-low: #f0f4f7;
    --surface-container: #eaeef1;
    --surface-high: #dfe3e6;
    --secondary-container: #bfe6f6;
    --card: #ffffff;
    --brand: #1565c0;
    --brand-2: #0e4481;
    --ok: #2e7d32;
    --error: #ba1a1a;
  }
  * { box-sizing: border-box; }
  html, body { margin: 0; padding: 0; }
  body {
    font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
    color: var(--ink);
    background: var(--paper);
    line-height: 1.55;
    letter-spacing: 0.01em;
  }
  body[dir="rtl"] {
    font-family: "Segoe UI", Tahoma, "Noto Sans Arabic", system-ui, sans-serif;
  }
  a { color: var(--brand); }
  .hidden { display: none; }
}

@layer layout {
  .wrap {
    width: min(1080px, calc(100% - 32px));
    margin: 0 auto;
  }
  main.wrap {
    container-type: inline-size;
    container-name: page;
  }
  header {
    background: var(--card);
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: 0;
    z-index: 30;
  }
  .bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    min-height: 72px;
  }
  .bar-end { display: flex; align-items: center; gap: 12px; }
  .hero {
    padding: clamp(32px, 6vw, 56px) 0 24px;
    view-transition-name: hero;
    & h1 {
      font-size: clamp(32px, 5vw, 48px);
      line-height: 1.12;
      margin: 0 0 12px;
      letter-spacing: -0.02em;
    }
  }
  .lead { font-size: clamp(16px, 2vw, 18px); color: var(--muted); max-width: 680px; }
  .grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 16px;
    padding: 12px 0 40px;
  }
  @container page (min-width: 720px) {
    .grid { grid-template-columns: repeat(3, 1fr); }
  }
  footer {
    border-top: 1px solid var(--line);
    padding: 24px 0 40px;
    color: var(--muted);
    display: flex;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
  }
}

@layer components {
  .brand {
    display: flex;
    align-items: center;
    gap: 12px;
    font-weight: 700;
    font-size: 20px;
    & img { width: 40px; height: 40px; border-radius: 8px; }
  }
  button.lang, a.lang {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--line);
    background: var(--card);
    color: var(--ink);
    text-decoration: none;
    font: inherit;
    border-radius: 999px;
    padding: 8px 14px;
    cursor: pointer;
    &:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
  }
  a.back {
    width: 40px;
    height: 40px;
    padding: 0;
  }
  html[dir="rtl"] a.back svg { transform: scaleX(-1); }
  .actions { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 20px; }
  .store {
    display: inline-block;
    background: var(--brand);
    color: white;
    text-decoration: none;
    border: 0;
    border-radius: 12px;
    padding: 12px 18px;
    font-weight: 600;
    cursor: pointer;
    &:hover { background: var(--brand-2); }
    &:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
    &[aria-busy="true"] { opacity: 0.7; }
  }
  a.quiet, button.quiet {
    display: inline-flex;
    align-items: center;
    background: transparent;
    color: var(--brand);
    text-decoration: none;
    border: 1px solid var(--brand);
    border-radius: 12px;
    padding: 12px 18px;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    &:hover { background: var(--secondary-container); }
    &:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
  }
  button.lang[aria-pressed="true"] {
    background: var(--brand);
    color: white;
    border-color: var(--brand);
  }
  .card, .price {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 18px;
    & h2 { margin: 0 0 8px; font-size: 18px; }
    & p { margin: 0; color: var(--muted); }
  }
  .shot {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 12px;
    overflow: hidden;
    margin-bottom: 28px;
    & img { display: block; width: 100%; height: auto; }
    & figcaption { padding: 12px 16px; color: var(--muted); }
  }
  .fee {
    font-size: clamp(28px, 4vw, 32px);
    font-weight: 700;
    margin: 8px 0;
    color: var(--ink);
    & span { font-size: 16px; font-weight: 500; color: var(--muted); }
  }
}

@layer forms {
  form label { display: grid; gap: 6px; margin: 12px 0; }
  input, select {
    display: block;
    width: min(420px, 100%);
    margin-top: 0;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--card);
    color: var(--ink);
    &:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
    &:user-invalid { border-color: #ba1a1a; }
  }
  .message { min-height: 1.5em; color: var(--ink); }
}

@layer dashboard {
  .toolbar { display: flex; gap: 12px; margin: 8px 0 16px; }
  #list { view-transition-name: dashboard; }
  .steps {
    list-style: none;
    margin: 12px 0 0;
    padding: 0;
  }
  .step {
    margin: 0;
    padding: 8px 0 8px 14px;
    border-inline-start: 2px solid var(--line);
    color: var(--muted);
    &.done {
      color: var(--ink);
      border-inline-start-color: var(--ok);
    }
  }
  .card + .card { margin-top: 12px; }
  #directConfirmed > .card { margin-top: 12px; }
  .owner-row > summary,
  .owner-group > summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    cursor: pointer;
    list-style: none;
    font-weight: 700;
  }
  .owner-row > summary::-webkit-details-marker,
  .owner-group > summary::-webkit-details-marker { display: none; }
  .owner-row > summary::marker,
  .owner-group > summary::marker { content: ""; }
  .owner-group > summary .owner-title {
    min-width: 0;
    unicode-bidi: isolate;
  }
  .owner-group > summary .owner-meta {
    display: flex;
    flex: none;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: flex-end;
  }
  .owner-row .chip,
  .owner-group .chip {
    flex: none;
    border-radius: 999px;
    padding: 4px 10px;
    background: var(--paper);
    border: 1px solid var(--line);
    color: var(--ink);
    font-size: 13px;
    font-weight: 600;
    white-space: nowrap;
    unicode-bidi: isolate;
  }
  .owner-row .chip.chip-reject {
    background: var(--paper);
    border-color: #e2a8a8;
    color: #b42318;
  }
  .owner-row .chip.chip-ok,
  .owner-group .chip.chip-ok {
    background: #e7f6ec;
    border-color: #9dcbad;
    color: #1b5e2a;
  }
  .partner-status {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 8px;
  }
  .partner-status .chip {
    border: 0;
    border-radius: 8px;
    padding: 6px 12px;
    background: var(--paper);
    color: var(--ink);
    font-size: 13px;
    font-weight: 600;
    white-space: nowrap;
  }
  .partner-status .chip.chip-wait {
    background: #fff4e5;
    color: #8a4b08;
  }
  .partner-status .chip.chip-ok {
    background: #e7f6ec;
    color: #1b5e2a;
  }
  .partner-status .chip.chip-reject {
    background: #fdecec;
    color: #b42318;
  }
  button.lang .partner-total {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 1.75em;
    height: 1.75em;
    margin-inline-start: 8px;
    padding: 0 6px;
    border-radius: 999px;
    background: #fff4e5;
    color: #8a4b08;
    font-size: 13px;
    font-weight: 700;
    line-height: 1;
  }
  button.lang[aria-pressed="true"] .partner-total {
    background: #fff4e5;
    color: #8a4b08;
  }
  .reject-note {
    margin: 8px 0 0;
    color: #b42318;
    font-weight: 600;
  }
  .reject-note.since {
    font-weight: 500;
    font-size: 13px;
  }
  .ok-note {
    margin: 8px 0 0;
    color: #1b5e2a;
    font-weight: 600;
  }
  .reject-banner,
  .ok-banner {
    margin-top: 10px;
    padding: 10px 12px;
    border-radius: 10px;
    font-weight: 600;
  }
  .reject-banner {
    background: #fde8e8;
    border: 1px solid #e2a8a8;
    color: #8a1f1f;
  }
  .ok-banner {
    background: #e7f6ec;
    border: 1px solid #9dcbad;
    color: #1b5e2a;
  }
  .reject-banner .since,
  .ok-banner .since {
    display: block;
    margin-top: 4px;
    font-size: 13px;
  }
  .attempt-list {
    margin-top: 12px;
    padding-top: 8px;
    border-top: 1px solid var(--line);
  }
  .attempt-list .attempt-heading {
    margin: 0 0 6px;
    font-weight: 700;
  }
  .attempt-list .attempt-line {
    margin: 0 0 4px;
    color: var(--muted);
    font-size: 13px;
  }
  .owner-row .owner-body { margin-top: 12px; }
  .owner-table-wrap {
    margin: 0 0 12px;
    border: 2px solid var(--line);
    border-radius: 12px;
    background: var(--card);
    overflow: hidden;
    box-shadow: 0 1px 0 rgba(23, 28, 30, 0.04);
  }
  .owner-table {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
  }
  .owner-table tbody th,
  .owner-table tbody td {
    padding: 11px 14px;
    border: 1px solid var(--line);
    vertical-align: top;
    overflow-wrap: anywhere;
    text-align: start;
  }
  .owner-table tbody th {
    width: 34%;
    max-width: 11rem;
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
    background: var(--surface-low);
    border-inline-end-width: 2px;
  }
  .owner-table tbody td {
    font-size: 14px;
    font-weight: 600;
    color: var(--ink);
    background: var(--card);
  }
  .owner-table tbody td.owner-code-cell {
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    font-size: 13px;
    letter-spacing: 0.04em;
  }
  .owner-table tbody td a {
    font-weight: 600;
    text-decoration: none;
  }
  .owner-table tbody td a:hover { text-decoration: underline; }
  @media (max-width: 640px) {
    .owner-table tbody th { width: 40%; }
  }
  .card .owner-row {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--line);
  }
  .empty {
    margin: 0;
    padding: 18px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--card);
    color: var(--muted);
  }
  .toolbar { flex-wrap: wrap; }
}

@media (max-width: 640px) {
  html, body { overflow-x: clip; }
  .bar { flex-wrap: wrap; row-gap: 8px; }
  .brand { min-width: 0; overflow-wrap: anywhere; }
  .bar-end { flex-wrap: wrap; }
  .toolbar { flex-direction: column; align-items: stretch; }
  .search-field {
    flex: 1 1 220px;
    min-width: 160px;
    max-width: 420px;
    margin: 0;
  }
  .search-field input {
    width: 100%;
    border: 1px solid color-mix(in srgb, CanvasText 18%, transparent);
    border-radius: 10px;
    padding: 0.55rem 0.85rem;
    background: Canvas;
    color: CanvasText;
    font: inherit;
  }
  .search-field input:focus {
    outline: 2px solid color-mix(in srgb, LinkText 55%, transparent);
    outline-offset: 1px;
  }
  .toolbar > button,
  .actions > button,
  .actions > a,
  .card button {
    width: 100%;
    min-height: 44px;
    overflow-wrap: anywhere;
  }
  input, select {
    width: 100%;
    max-width: 100%;
    font-size: 16px;
  }
  .card, .price, .empty, .card h2, .card p { overflow-wrap: anywhere; }
}

.hidden { display: none; }

.account-slot { position: relative; display: inline-flex; }
.account-menu {
  position: absolute;
  top: calc(100% + 6px);
  inset-inline-end: 0;
  z-index: 20;
  min-width: 180px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 6px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.account-menu.hidden { display: none; }
.account-menu a,
.account-menu button {
  display: block;
  width: 100%;
  margin: 0;
  padding: 10px 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  text-align: start;
  text-decoration: none;
  cursor: pointer;
}
.account-menu a:hover,
.account-menu button:hover { background: var(--paper); }
.dial-slot {
  display: flex;
  align-items: center;
  gap: 8px;
  width: min(420px, 100%);
}
.dial-slot input { flex: 1; width: auto; min-width: 0; }
.dial-badge {
  flex: none;
  color: var(--brand);
  font-weight: 700;
  font-size: 14px;
}
.dial-ask {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(23, 28, 30, 0.4);
}
.dial-ask-card { width: min(420px, 100%); }
.dial-ask-card p { margin: 8px 0; }

/* Reject / cancel reason dialog — outside @layer so it beats form label grid */
.reject-dialog {
  width: min(440px, calc(100% - 32px));
  max-width: 440px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--card);
  color: #102027;
  box-shadow: 0 12px 40px rgba(16, 32, 39, 0.18);
}
.reject-dialog::backdrop {
  background: rgba(23, 28, 30, 0.45);
}
.reject-dialog > h2 {
  margin: 0;
  padding: 20px 20px 8px;
  font-size: 20px;
  font-weight: 700;
  color: #102027;
}
.reject-dialog form.reject-form {
  display: flex;
  flex-direction: column;
  max-height: min(70vh, 560px);
  margin: 0;
}
.reject-dialog .reject-scroll {
  flex: 1 1 auto;
  overflow: auto;
  padding: 0 16px 8px;
}
.reject-dialog .reason-group-title {
  margin: 12px 0 4px;
  font-size: 15px;
  font-weight: 700;
  color: #102027;
}
.reject-dialog label.reason-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 0;
  padding: 8px 4px;
  border-radius: 10px;
  cursor: pointer;
  font: inherit;
  color: #102027;
}
.reject-dialog label.reason-row:hover {
  background: var(--surface-low);
}
.reject-dialog label.reason-row input[type="radio"] {
  flex: none;
  width: 18px;
  height: 18px;
  margin: 2px 0 0;
  accent-color: var(--brand);
}
.reject-dialog label.reason-row .reason-text {
  flex: 1;
  min-width: 0;
  font-size: 15px;
  line-height: 1.35;
  text-align: start;
}
.reject-dialog label.reject-note-label {
  display: block;
  margin: 12px 0 6px;
  font-weight: 600;
  color: #102027;
}
.reject-dialog textarea.reject-note-input {
  display: block;
  width: 100%;
  max-width: 100%;
  min-height: 72px;
  margin: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: var(--ink);
  font: inherit;
  resize: vertical;
}
.reject-dialog textarea.reject-note-input:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
}
.reject-dialog .reject-error {
  margin: 8px 0 0;
  color: #b00020;
  font-weight: 600;
  font-size: 14px;
  min-height: 1.2em;
}
.reject-dialog .actions.reject-actions {
  display: flex;
  gap: 12px;
  flex-wrap: nowrap;
  margin: 0;
  padding: 12px 16px 16px;
  border-top: 1px solid #d7e3ea;
  flex: 0 0 auto;
}
.reject-dialog .actions.reject-actions > button {
  flex: 1 1 0;
  width: auto;
  min-width: 0;
}
