:root {
  --ink: #0f172a;
  --muted: #64748b;
  --soft: #94a3b8;
  --line: #e2e8f0;
  --brand: #0ea5e9;
  --brand2: #0284c7;
  --brand-bg: #f0f9ff;
  --good: #047857;
  --good-bg: #ecfdf5;
  --good-line: #a7f3d0;
  --page: #f1f5f9;
  --card: #ffffff;
  --radius: 1rem;
  --gutter: clamp(1rem, 4vw, 3rem);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

/* The whole UI is sized in rem; app.js shrinks the root font-size
   when a viewport is too small, so nothing is ever cut off. */
html { font-size: 16px; }
@media (min-width: 1500px) and (min-height: 900px) { html { font-size: 17px; } }

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: "Cairo", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--page);
  -webkit-font-smoothing: antialiased;
}

button, input { font: inherit; color: inherit; }

/* Number inputs without spinners */
input[type="number"] { -moz-appearance: textfield; appearance: textfield; }
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }

/* ===== Screens (each fills the whole page, never scrolls) ===== */
.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  overflow: hidden;
}
.screen.is-active { display: flex; }

[data-fit] { overflow: hidden; min-height: 0; }

/* ===== Home ===== */
.screen--home {
  background:
    radial-gradient(60rem 40rem at 85% -10%, rgba(255, 255, 255, 0.18), transparent 60%),
    linear-gradient(160deg, #0ea5e9 0%, #0369a1 100%);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.home__inner {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 1.5rem var(--gutter);
}
/* auto margins center the content but never push it off the top edge */
.home__head { margin-top: auto; text-align: center; color: #fff; }
.modes { margin-bottom: auto; }

.home__head h1 {
  font-size: clamp(1.6rem, 4.5vw, 2.75rem);
  font-weight: 800;
  line-height: 1.3;
}
.home__head p {
  margin-top: 0.4rem;
  color: #e0f2fe;
  font-size: 1.05rem;
}

.modes {
  width: 100%;
  max-width: 68rem;
  margin-inline: auto;
  margin-top: clamp(1.5rem, 6vh, 3.5rem);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: clamp(0.75rem, 2vw, 1.5rem);
}
.mode {
  cursor: pointer;
  background: var(--card);
  border: none;
  border-radius: var(--radius);
  padding: clamp(1.5rem, 5vh, 2.75rem) 1.25rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  text-align: center;
  box-shadow: 0 0.75rem 2rem rgba(3, 60, 100, 0.22);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.mode:hover,
.mode:focus-visible {
  transform: translateY(-4px);
  box-shadow: 0 1.25rem 2.75rem rgba(3, 60, 100, 0.32);
  outline: none;
}
.mode:focus-visible { box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--brand2); }

.mode__icon {
  flex: none;
  width: 4rem;
  height: 4rem;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--brand-bg);
  color: var(--brand2);
}
.mode__icon svg {
  width: 2rem;
  height: 2rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.mode__text { display: flex; flex-direction: column; gap: 0.3rem; }
.mode__title { font-weight: 800; font-size: 1.3rem; }
.mode__desc { font-size: 0.88rem; color: var(--muted); line-height: 1.5; }
.mode__go { display: none; }

/* ===== Detail ===== */
.screen--detail { background: var(--page); }

.bar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  padding: 0.75rem var(--gutter);
  padding-top: max(0.75rem, env(safe-area-inset-top));
}
.bar h2 {
  flex: 1;
  min-width: 0;
  font-size: 1.35rem;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.back {
  cursor: pointer;
  flex: none;
  font-weight: 700;
  font-size: 0.92rem;
  color: var(--brand2);
  background: var(--brand-bg);
  border: 1px solid #bae6fd;
  border-radius: 0.65rem;
  height: 2.5rem;
  padding: 0 0.85rem;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  transition: background 0.12s ease;
}
.back:hover { background: #e0f2fe; }
.back span[aria-hidden] { font-size: 1.4rem; line-height: 1; margin-top: -0.15rem; }

.rate {
  flex: none;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 600;
}
.rate input {
  width: 6.5rem;
  height: 2.5rem;
  padding: 0 0.6rem;
  border: 1px solid var(--line);
  border-radius: 0.65rem;
  background: var(--card);
  font-weight: 700;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.rate input:focus { outline: none; border-color: var(--brand); background: #fff; }

/* Panel: inputs + results split the remaining page */
.panel {
  flex: 1;
  min-height: 0;
  display: none;
  grid-template-columns: minmax(0, 1fr) minmax(20rem, 38%);
}
.panel.is-active { display: grid; }

.inputs,
.output {
  display: flex;
  flex-direction: column;
  padding: clamp(1.25rem, 5vh, 3rem) var(--gutter);
  padding-bottom: max(clamp(1.25rem, 5vh, 3rem), env(safe-area-inset-bottom));
}
.inputs__inner,
.output__inner {
  margin-block: auto;      /* vertical centering that never clips the top */
  width: 100%;
}
.inputs__inner { max-width: 40rem; margin-inline: auto; }
.output__inner { max-width: 30rem; margin-inline: auto; }

.output {
  background: var(--card);
  border-inline-start: 1px solid var(--line);
}

.eyebrow {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--soft);
  letter-spacing: 0.02em;
  margin-bottom: 1rem;
}

/* Fields */
.fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1.1rem 1rem;
}
.field { display: flex; flex-direction: column; gap: 0.45rem; min-width: 0; }
.field--wide { grid-column: 1 / -1; }
.field__label { font-size: 0.88rem; color: var(--muted); font-weight: 600; }
.field__box {
  display: flex;
  align-items: center;
  height: 3.4rem;
  background: var(--card);
  border: 1.5px solid var(--line);
  border-radius: 0.8rem;
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}
.field__box:focus-within {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(14, 165, 233, 0.15);
}
.field__box input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: none;
  background: transparent;
  padding: 0 1rem;
  font-size: 1.15rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.field__box input:focus { outline: none; }
.field__box input::placeholder { color: var(--soft); font-weight: 500; font-size: 0.95rem; }

/* A required field still empty after the user started filling the form */
:root { --warn: #c2410c; --warn-bg: #fff7ed; --warn-line: #fdba74; }
.field__label { display: flex; align-items: center; gap: 0.4rem; }
.field.is-missing .field__label::after,
.rate.is-missing > span::after {
  content: "مطلوب";
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--warn);
  background: var(--warn-bg);
  border-radius: 999px;
  padding: 0 0.45rem;
}
.field.is-missing .field__box,
.rate.is-missing input { border-color: var(--warn-line); background: var(--warn-bg); }
.unit {
  flex: none;
  padding-inline-end: 1rem;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--soft);
}

/* Formula (طريقة الحساب): inline under the inputs when it fits,
   otherwise collapsed to a button that opens it over the inputs. */
.inputs { position: relative; }
.steps { margin-top: 1.75rem; }
.steps__toggle { display: none; list-style: none; }
.steps__toggle::-webkit-details-marker { display: none; }
.steps__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.6rem;
}
.steps__head .eyebrow { margin: 0; }
.steps__close { display: none; }
.steps__list { display: flex; flex-direction: column; gap: 0.5rem; }
.step {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.6rem 0.9rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 0.75rem;
}
.step__label { font-size: 0.78rem; font-weight: 600; color: var(--muted); }
.step__expr {
  display: block;
  direction: ltr;
  text-align: right;
  font-size: 0.92rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  overflow-wrap: anywhere;
}
.step__expr b { color: var(--brand2); font-weight: 800; }
.step__formula {
  display: block;
  direction: ltr;
  text-align: right;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
}

.steps.is-compact { margin-top: 1rem; }
.steps.is-compact .steps__toggle,
.steps.is-compact .steps__close {
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--brand2);
  background: var(--brand-bg);
  border: 1px solid #bae6fd;
  border-radius: 999px;
  padding: 0.3rem 0.9rem;
}
.steps.is-compact .steps__toggle::before { content: "∑"; font-size: 0.95rem; }
.steps.is-compact[open] .steps__toggle { visibility: hidden; }
.steps.is-compact .steps__body {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  padding: 1rem var(--gutter);
  background: var(--page);
}
.steps:not([open]) .steps__body { display: none; }  /* after the rule above, so it wins */

/* Result: empty state until every field is filled */
.empty {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1.25rem;
  border: 1.5px dashed var(--line);
  border-radius: 0.9rem;
  text-align: center;
}
.empty__title { font-weight: 700; color: var(--muted); }
.empty__list { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.4rem; }
.empty__list span {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  background: var(--page);
  border-radius: 999px;
  padding: 0.2rem 0.75rem;
}
.empty--started { border-color: var(--warn-line); background: var(--warn-bg); }
.empty--started .empty__title { color: var(--warn); }
.empty--started .empty__list span { background: #fff; color: var(--warn); }
.steps__empty {
  padding: 0.8rem 1rem;
  border: 1.5px dashed var(--line);
  border-radius: 0.75rem;
  font-size: 0.85rem;
  color: var(--soft);
}
.out.out--need .out__val { font-size: 1rem; color: var(--warn); }

/* Result */
.result { display: flex; flex-direction: column; gap: 0.75rem; }
.out {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding: 1rem 1.25rem;
  border-radius: 0.9rem;
  border: 1px solid var(--line);
}
.out__label { font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.out__val {
  font-weight: 800;
  font-size: 1.6rem;
  line-height: 1.35;
  color: var(--brand2);
  font-variant-numeric: tabular-nums;
  direction: ltr;
  text-align: right;
  white-space: nowrap;   /* never break a number; app.js shrinks the UI if it can't fit */
}
.out--primary { background: var(--good-bg); border-color: var(--good-line); }
.out--primary .out__val { color: var(--good); font-size: 2.1rem; }
.out--invalid { background: #fff7ed; border-color: #fed7aa; }
.out--invalid .out__label { color: #9a3412; white-space: normal; }
.out--invalid .out__val { color: #c2410c; }

.meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.25rem;
}
.meta span {
  font-size: 0.8rem;
  color: var(--muted);
  background: var(--page);
  border-radius: 999px;
  padding: 0.2rem 0.7rem;
}
.meta b { color: var(--ink); font-weight: 700; }

/* ===== Narrow portrait screens: stack inputs over a bottom result sheet ===== */
@media (max-width: 760px) and (orientation: portrait) {
  :root { --gutter: 1rem; }

  .modes { grid-template-columns: 1fr; max-width: 30rem; }
  .mode {
    flex-direction: row;
    text-align: start;
    padding: 1rem 1.1rem;
    gap: 0.9rem;
  }
  .mode__icon { width: 3rem; height: 3rem; }
  .mode__icon svg { width: 1.5rem; height: 1.5rem; }
  .mode__text { flex: 1; gap: 0.1rem; }
  .mode__title { font-size: 1.1rem; }
  .mode__desc { font-size: 0.8rem; }
  .mode__go { display: block; font-size: 1.6rem; color: var(--soft); line-height: 1; }

  .bar { gap: 0.5rem; padding-block: 0.6rem; }
  .bar h2 { font-size: 1.1rem; }
  .back { padding: 0 0.6rem; height: 2.3rem; }
  .back__text { display: none; }
  .rate { flex-direction: column; align-items: stretch; gap: 0.1rem; font-size: 0.68rem; }
  .rate input { width: 5.75rem; height: 2rem; font-size: 0.9rem; }

  .panel { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto; }
  .inputs { padding-block: 1rem; }
  .output {
    border-inline-start: none;
    border-top: 1px solid var(--line);
    border-radius: 1.25rem 1.25rem 0 0;
    box-shadow: 0 -0.5rem 1.5rem rgba(15, 23, 42, 0.06);
    padding-top: 1rem;
    padding-bottom: max(1rem, env(safe-area-inset-bottom));
  }
  .eyebrow { margin-bottom: 0.6rem; }
  .fields { gap: 0.75rem; }
  .field { gap: 0.3rem; }
  .field__label { font-size: 0.8rem; }
  .field__box { height: 2.9rem; }
  .field__box input { padding: 0 0.75rem; font-size: 1.05rem; }
  .unit { padding-inline-end: 0.75rem; font-size: 0.78rem; }

  .result { gap: 0.5rem; }
  .out {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.7rem 1rem;
  }
  .out--primary { flex-direction: column; align-items: stretch; gap: 0.1rem; }
  .out__val { font-size: 1.25rem; }
  .out--primary .out__val { font-size: 1.65rem; }
}

/* Short landscape screens (phones turned sideways) */
@media (max-height: 500px) and (orientation: landscape) {
  .bar { padding-block: 0.5rem; }
  .inputs, .output { padding-block: 1rem; }
  .field__box { height: 2.8rem; }
  .fields { gap: 0.75rem 1rem; }
  .eyebrow { margin-bottom: 0.6rem; }
  .out { padding: 0.65rem 1rem; }
  .out--primary .out__val { font-size: 1.6rem; }
  .out__val { font-size: 1.3rem; }
}
