/* Mai — CPME client UI. All values come from brand/tokens.css.
   Layout uses logical properties so Arabic (RTL) and English (LTR) share one set of rules. */
*,
*::before,
*::after {
  box-sizing: border-box;
}
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
html,
body {
  height: 100%;
}
body {
  margin: 0;
  color: var(--color-text);
  background: var(--color-background);
  font: 400 var(--text-md)/1.5 var(--font-sans);
  -webkit-font-smoothing: antialiased;
  overflow-wrap: break-word;
}
:lang(ar),
[dir='rtl'] {
  font-family: var(--font-arabic);
}
h1,
h2,
p {
  margin: 0;
}
button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}
button {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
[hidden] {
  display: none !important;
}
svg {
  fill: currentColor;
}
:focus {
  outline: none;
}
:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---- Brand surfaces & logo -------------------------------------------- */
.brand-logo {
  display: block;
  height: var(--logo-height);
  width: auto;
  flex: none;
}
.brand-band {
  display: grid;
  place-items: center;
  padding: var(--space-5) var(--space-5);
  background: var(--color-ink);
}
.brand-band .brand-logo {
  height: 44px;
}

/* ---- Buttons ------------------------------------------------------------ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--touch);
  padding: 0 var(--space-5);
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-weight: 500;
  line-height: 1.2;
  text-align: center;
  transition:
    background-color var(--dur-fast) var(--ease),
    border-color var(--dur-fast) var(--ease),
    transform var(--dur-fast) var(--ease);
}
.btn svg {
  width: 20px;
  height: 20px;
  flex: none;
}
.btn:active:not(:disabled) {
  transform: scale(0.98);
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn-primary {
  color: var(--color-on-primary);
  background: var(--color-primary-deep);
}
.btn-primary:hover:not(:disabled) {
  background: var(--color-primary-deeper);
}
.btn-soft {
  color: var(--color-primary-deeper);
  background: var(--color-primary-soft);
}
.btn-soft:hover:not(:disabled) {
  background: #d9eaf3;
}
.btn-ghost {
  color: var(--color-primary-deep);
  background: transparent;
  border-color: var(--color-border-strong);
}
.btn-ghost:hover:not(:disabled) {
  background: var(--color-surface-sunken);
}
.btn-link {
  min-height: var(--touch);
  padding: 0 var(--space-3);
  border: 0;
  color: var(--color-primary-deep);
  background: none;
  font-weight: 500;
  border-radius: var(--radius-sm);
}
.btn-link:hover {
  text-decoration: underline;
}

/* ---- Forms -------------------------------------------------------------- */
.field-label {
  display: block;
  margin-bottom: var(--space-2);
  color: var(--color-text-secondary);
  font-size: var(--text-sm);
  font-weight: 500;
}
input[type='text'],
input[type='password'],
input[type='search'],
select,
textarea {
  width: 100%;
  min-height: var(--touch);
  padding: 10px var(--space-4);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius);
  background: var(--color-surface);
  font-size: 16px; /* 16px stops iOS from zooming on focus */
  transition:
    border-color var(--dur-fast) var(--ease),
    box-shadow var(--dur-fast) var(--ease);
}
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  border-color: var(--color-focus);
  box-shadow: 0 0 0 3px rgba(43, 102, 132, 0.22);
}
.form-error {
  color: var(--color-danger);
  font-size: var(--text-sm);
}

/* ---- App shell & top bar ------------------------------------------------ */
.app-shell {
  display: flex;
  flex-direction: column;
  height: var(--app-height, 100dvh);
}
.topbar {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex: none;
  min-height: var(--header-height);
  padding: env(safe-area-inset-top) max(var(--space-4), env(safe-area-inset-right)) 0
    max(var(--space-4), env(safe-area-inset-left));
  color: #fff;
  background: var(--color-ink);
}
.topbar-divider {
  width: 1px;
  height: 22px;
  background: rgba(255, 255, 255, 0.28);
}
.app-name {
  font-size: var(--text-md);
  font-weight: 500;
  letter-spacing: 0.02em;
}
.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  margin-inline-start: auto;
}
.topbar-button {
  display: grid;
  place-items: center;
  min-width: var(--touch);
  min-height: var(--touch);
  padding: 0 var(--space-3);
  border: 0;
  border-radius: var(--radius);
  color: inherit;
  background: transparent;
  font-size: var(--text-sm);
  font-weight: 500;
  transition: background-color var(--dur-fast) var(--ease);
}
.topbar-button:hover {
  background: rgba(255, 255, 255, 0.12);
}
.topbar-button svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.topbar-button:focus-visible {
  box-shadow:
    0 0 0 2px var(--color-ink),
    0 0 0 4px var(--color-accent);
}
.topbar-button.on-light {
  color: var(--color-primary-deep);
}
.topbar-button.on-light:hover {
  background: var(--color-surface-sunken);
}
.topbar-button.on-light:focus-visible {
  box-shadow: var(--focus-ring);
}

/* ---- Conversation ------------------------------------------------------- */
.chat-area {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-height: 0;
}
.conversation {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: var(--space-5) max(var(--space-4), env(safe-area-inset-right)) var(--space-4)
    max(var(--space-4), env(safe-area-inset-left));
  scroll-behavior: smooth;
}
.welcome-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  min-height: 100%;
  padding: var(--space-5) 0;
  text-align: center;
}
.welcome-state h1 {
  font-size: var(--text-xl);
  line-height: 1.2;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.welcome-copy {
  max-width: 32ch;
  color: var(--color-text-secondary);
}
.welcome-state .btn {
  margin-top: var(--space-2);
}
.message-row {
  display: flex;
  flex-direction: column;
  width: min(100%, 760px);
  margin: 0 auto var(--space-4);
  animation: message-in var(--dur) var(--ease) both;
}
.message-row.user {
  align-items: flex-end;
}
.message-row.assistant {
  align-items: flex-start;
}
.message-bubble {
  max-width: min(100%, 68ch);
  padding: 10px var(--space-4);
  border-radius: var(--radius-lg);
  font-size: var(--text-md);
  line-height: 1.65;
  white-space: pre-wrap;
  text-align: start;
  unicode-bidi: plaintext;
}
.user .message-bubble {
  color: var(--color-on-primary);
  background: var(--color-primary-deep);
  border-end-end-radius: var(--space-1);
}
.assistant .message-bubble {
  color: var(--color-text);
  background: var(--color-surface-sunken);
  border-end-start-radius: var(--space-1);
}
.message-bubble.is-error {
  color: var(--color-danger);
  background: var(--color-danger-soft);
}
.thinking-bubble {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--color-text-muted);
}
.thinking-dots {
  display: inline-flex;
  gap: 4px;
}
.thinking-dots i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  animation: dot 1.2s ease-in-out infinite;
}
.thinking-dots i:nth-child(2) {
  animation-delay: 0.15s;
}
.thinking-dots i:nth-child(3) {
  animation-delay: 0.3s;
}
@keyframes dot {
  0%,
  80%,
  100% {
    opacity: 0.3;
    transform: translateY(0);
  }
  40% {
    opacity: 1;
    transform: translateY(-3px);
  }
}
@keyframes message-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* ---- Composer ----------------------------------------------------------- */
.composer-wrap {
  flex: none;
  width: min(100%, 760px);
  margin: 0 auto;
  padding: var(--space-2) max(var(--space-4), env(safe-area-inset-right))
    max(var(--space-3), env(safe-area-inset-bottom)) max(var(--space-4), env(safe-area-inset-left));
}
.composer {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  min-height: 56px;
  padding: var(--space-1) var(--space-1) var(--space-1) var(--space-4);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-pill);
  background: var(--color-surface);
  box-shadow: var(--shadow-sm);
  transition:
    border-color var(--dur-fast) var(--ease),
    box-shadow var(--dur-fast) var(--ease);
}
[dir='rtl'] .composer {
  padding: var(--space-1) var(--space-4) var(--space-1) var(--space-1);
}
.composer:focus-within {
  border-color: var(--color-focus);
  box-shadow: 0 0 0 3px rgba(43, 102, 132, 0.18);
}
.composer input {
  flex: 1;
  min-width: 0;
  min-height: var(--touch);
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
}
.composer input::placeholder {
  color: var(--color-text-muted);
  opacity: 1;
}
.composer input:focus-visible {
  box-shadow: none;
}
.composer-icon,
.send-button {
  display: grid;
  place-items: center;
  flex: none;
  width: var(--touch);
  height: var(--touch);
  border: 0;
  border-radius: 50%;
  transition:
    background-color var(--dur-fast) var(--ease),
    transform var(--dur-fast) var(--ease);
}
.composer-icon {
  color: var(--color-primary-deep);
  background: transparent;
}
.composer-icon:hover {
  background: var(--color-primary-soft);
}
.composer-icon svg,
.send-button svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.send-button {
  color: var(--color-on-primary);
  background: var(--color-primary-deep);
}
.send-button:hover:not(:disabled) {
  background: var(--color-primary-deeper);
}
.send-button:active:not(:disabled) {
  transform: scale(0.94);
}
.send-button:disabled {
  background: var(--color-border-strong);
  cursor: not-allowed;
}
[dir='rtl'] .send-button svg,
[dir='rtl'] .back-button svg {
  transform: scaleX(-1);
}
.composer-hint {
  margin-top: var(--space-2);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  text-align: center;
}

/* ---- Orb ---------------------------------------------------------------- */
.hero-orb {
  position: relative;
  overflow: hidden;
  flex: none;
  border-radius: 50%;
  background: radial-gradient(
    circle at 50% 20%,
    #fff 0,
    var(--color-accent) 60%,
    var(--color-primary) 100%
  );
}
.hero-orb canvas {
  display: block;
  width: 100%;
  height: 100%;
}
.orb-small {
  width: 104px;
  height: 104px;
  margin-bottom: var(--space-2);
  cursor: pointer;
  transition: transform var(--dur) var(--ease);
}
.orb-small:hover {
  transform: scale(1.04);
}
.orb-small:focus-visible {
  box-shadow: var(--focus-ring);
}
.orb-large {
  width: min(62vw, 38dvh, 320px);
  height: min(62vw, 38dvh, 320px);
  cursor: pointer;
}
.orb-large:focus-visible {
  box-shadow: var(--focus-ring);
}

/* ---- Voice panel -------------------------------------------------------- */
.voice-panel {
  position: fixed;
  z-index: 20;
  inset: 0;
  display: flex;
  flex-direction: column;
  height: var(--app-height, 100dvh);
  overflow: hidden;
  background: var(--color-background);
  animation: voice-in var(--dur) var(--ease) both;
}
.voice-header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  flex: none;
  min-height: var(--header-height);
  padding: env(safe-area-inset-top) max(var(--space-3), env(safe-area-inset-right)) 0
    max(var(--space-3), env(safe-area-inset-left));
  border-bottom: 1px solid var(--color-border);
}
.voice-header > :last-child {
  justify-self: end;
}
.voice-header-actions {
  display: flex;
  align-items: center;
  justify-content: end;
  gap: var(--space-1);
}
.voice-title {
  font-size: var(--text-md);
  font-weight: 700;
}
.back-button {
  display: inline-flex;
  white-space: nowrap;
  align-items: center;
  gap: var(--space-1);
  justify-self: start;
  min-height: var(--touch);
  padding: 0 var(--space-3) 0 var(--space-2);
  border: 0;
  border-radius: var(--radius);
  color: var(--color-primary-deep);
  background: transparent;
  font-weight: 500;
}
[dir='rtl'] .back-button {
  padding: 0 var(--space-2) 0 var(--space-3);
}
.back-button:hover {
  background: var(--color-surface-sunken);
}
.back-button svg {
  width: 22px;
  height: 22px;
}
.voice-stage {
  display: flex;
  flex: 1;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  min-height: 0;
  overflow-y: auto;
  padding: var(--space-4) max(var(--space-5), env(safe-area-inset-right)) var(--space-4)
    max(var(--space-5), env(safe-area-inset-left));
  text-align: center;
}
.orb-wrap {
  position: relative;
  display: grid;
  place-items: center;
  padding: var(--space-4);
  margin-bottom: var(--space-2);
}
@keyframes voice-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
.voice-status {
  font-size: var(--text-lg);
  font-weight: 700;
  line-height: 1.3;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.voice-hint:empty {
  display: none;
}
.voice-hint {
  color: var(--color-danger);
}
.voice-hint {
  max-width: 36ch;
  min-height: 1.5em;
  font-size: var(--text-sm);
}
.voice-transcript {
  width: min(100%, 560px);
}
.voice-transcript p {
  max-height: 7.5em;
  overflow-y: auto;
  margin-top: var(--space-2);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius);
  font-size: var(--text-md);
  line-height: 1.6;
  text-align: start;
  unicode-bidi: plaintext;
}
.recognized-text {
  color: var(--color-text-secondary);
  background: var(--color-surface-sunken);
}
.spoken-text {
  color: var(--color-text);
  background: var(--color-primary-soft);
}
.voice-action {
  min-width: 160px;
}

/* Suggested questions: a single strip of chips, scrolls sideways on narrow screens */
.quick-questions {
  flex: none;
  padding: var(--space-3) 0 max(var(--space-4), env(safe-area-inset-bottom));
  border-top: 1px solid var(--color-border);
}
.quick-questions #quick-question-list {
  display: flex;
  flex-wrap: nowrap;
  gap: var(--space-2);
  overflow-x: auto;
  scroll-snap-type: x proximity;
  scrollbar-width: none;
  padding: 2px max(var(--space-4), env(safe-area-inset-right)) 2px
    max(var(--space-4), env(safe-area-inset-left));
  -webkit-overflow-scrolling: touch;
}
.quick-questions #quick-question-list::-webkit-scrollbar {
  display: none;
}
.quick-question-column {
  display: contents;
}
.quick-questions-title {
  display: none;
}
.quick-question {
  flex: none;
  max-width: 78vw;
  min-height: var(--touch);
  padding: 8px var(--space-4);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-pill);
  color: var(--color-text);
  background: var(--color-surface);
  font-size: var(--text-sm);
  line-height: 1.35;
  text-align: start;
  scroll-snap-align: start;
  transition:
    border-color var(--dur-fast) var(--ease),
    background-color var(--dur-fast) var(--ease);
}
.quick-question:hover:not(:disabled) {
  border-color: var(--color-primary-deep);
  background: var(--color-primary-soft);
}
.quick-question:disabled {
  cursor: not-allowed;
  opacity: 0.4;
}
.document-tray {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  width: min(100%, 760px);
  margin: var(--space-2) auto 0;
}
.document-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-pill);
  color: var(--color-text-secondary);
  background: var(--color-surface-sunken);
  font-size: var(--text-sm);
}
.document-chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.document-chip button {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  color: var(--color-text-secondary);
  background: transparent;
  cursor: pointer;
}
.document-chip button:hover {
  color: var(--color-danger);
  background: var(--color-surface);
}
.document-status {
  width: min(100%, 760px);
  margin: var(--space-2) auto 0;
  color: var(--color-text-secondary);
  font-size: var(--text-sm);
  text-align: center;
}
@media (min-width: 900px) {
  .quick-questions #quick-question-list {
    flex-wrap: wrap;
    justify-content: center;
    overflow: visible;
    max-width: 920px;
    margin: 0 auto;
  }
  .quick-question {
    max-width: 360px;
  }
}
@media (max-height: 520px) {
  .quick-questions {
    display: none;
  }
  .orb-large {
    width: min(40vh, 220px);
    height: min(40vh, 220px);
  }
}
@media (max-width: 359px) {
  .back-button span {
    display: none;
  }
  .voice-header {
    grid-template-columns: auto 1fr auto;
  }
  .voice-title {
    text-align: center;
  }
}

/* ---- Secondary pages (admin) ------------------------------------------- */
.page-header {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-height: var(--header-height);
  padding: env(safe-area-inset-top) var(--space-4) 0;
  color: #fff;
  background: var(--color-ink);
}
.page-header a {
  display: flex;
}
.page-header .title {
  font-weight: 500;
}
.page-main {
  display: grid;
  gap: var(--space-4);
  width: min(100%, 960px);
  margin: 0 auto;
  padding: var(--space-5) var(--space-4) calc(var(--space-6) + env(safe-area-inset-bottom));
}
.stack {
  display: grid;
  gap: var(--space-4);
}
.card {
  padding: var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface);
}
.table-scroll {
  overflow-x: auto;
}
table {
  width: 100%;
  border-collapse: collapse;
}
th,
td {
  padding: var(--space-3) var(--space-2);
  border-bottom: 1px solid var(--color-border);
  font-size: var(--text-sm);
  text-align: start;
}
th {
  color: var(--color-text-secondary);
  font-weight: 500;
}
td:first-child {
  overflow-wrap: anywhere;
}
.drop {
  display: grid;
  justify-items: center;
  gap: var(--space-3);
  padding: var(--space-6) var(--space-4);
  border: 2px dashed var(--color-border-strong);
  border-radius: var(--radius);
  color: var(--color-text-secondary);
  text-align: center;
  transition:
    background-color var(--dur-fast) var(--ease),
    border-color var(--dur-fast) var(--ease);
}
.drop.over {
  border-color: var(--color-primary-deep);
  background: var(--color-primary-soft);
}
.notice {
  min-height: 1.5em;
  font-size: var(--text-sm);
}
.notice.is-error {
  color: var(--color-danger);
}
.inline-form {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: var(--space-3);
}
.inline-form > div {
  flex: 1 1 220px;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
