:root {
  --brand: #5f8533;
  --brand-dark: #4a6828;
  --brand-light: #eef3e6;
  --ink: #1f2418;
  --muted: #6b7280;
  --line: #dfe3d8;
  --surface: #ffffff;
  --bg: #f4f6f1;
  --danger: #b3261e;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 6px 18px rgba(0, 0, 0, .06);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

/* ---- app bar ---------------------------------------------------------- */

.appbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--brand);
  color: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .12);
}

.appbar h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: .01em;
}

.appbar .back {
  color: #fff;
  text-decoration: none;
  font-size: 1.4rem;
  line-height: 1;
  padding: 4px 8px;
  border-radius: 8px;
}

.appbar .back:hover { background: rgba(255, 255, 255, .15); }

/* ---- layout ----------------------------------------------------------- */

.wrap {
  max-width: 880px;
  margin: 0 auto;
  padding: 16px 16px 120px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.card > h2 {
  margin: 0 0 14px;
  font-size: .78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .09em;
  color: var(--brand-dark);
}

.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}

@media (min-width: 620px) {
  .grid.cols-2 { grid-template-columns: 1fr 1fr; }
  .grid.cols-3 { grid-template-columns: 1fr 1fr 1fr; }
  .span-2 { grid-column: span 2; }
}

/* ---- fields ----------------------------------------------------------- */

.field { display: flex; flex-direction: column; gap: 6px; }

.field label {
  font-size: .8rem;
  font-weight: 600;
  color: var(--muted);
}

/* 16px minimum stops iOS Safari zooming in on focus. */
input, select, textarea {
  font: inherit;
  font-size: 16px;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px 12px;
  width: 100%;
  min-height: 44px;
}

textarea { min-height: 92px; resize: vertical; }

/* Entered values print in capitals, so show them that way while typing.
   Date and number inputs are excluded -- transforming them does nothing useful
   and can interfere with native pickers. */
input[type="text"],
textarea,
select {
  text-transform: uppercase;
}

::placeholder { text-transform: none; }

input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--brand);
  outline-offset: 1px;
  border-color: var(--brand);
}

input[readonly] {
  background: var(--brand-light);
  font-weight: 700;
}

.input-with-suffix {
  display: flex;
  align-items: stretch;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  overflow: hidden;
}

.input-with-suffix input {
  border: none;
  border-radius: 0;
  min-width: 0;
  flex: 1;
}

.input-with-suffix input:focus {
  outline: none;
}

.input-with-suffix:focus-within {
  outline: 2px solid var(--brand);
  outline-offset: 1px;
  border-color: var(--brand);
}

.input-with-suffix .suffix {
  display: flex;
  align-items: center;
  padding: 0 12px;
  background: var(--brand-light);
  color: var(--brand-dark);
  font-weight: 600;
  font-size: .85rem;
  white-space: nowrap;
  border-left: 1px solid var(--line);
  text-transform: none;
}

.hint { font-size: .75rem; color: var(--muted); }

/* ---- buttons ---------------------------------------------------------- */

.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 12px 18px;
  min-height: 44px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }

.btn-secondary { background: #fff; color: var(--brand-dark); border-color: var(--brand); }
.btn-secondary:hover { background: var(--brand-light); }

.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: #f0f0ee; }

.btn-danger { background: transparent; color: var(--danger); border-color: #e6c9c7; }

.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-block { width: 100%; }

/* ---- medicine list ---------------------------------------------------- */

.med-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }

.med-row {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 10px 12px;
  background: var(--brand-light);
  border: 1px solid #dbe5cc;
  border-radius: 10px;
}

.med-row .idx {
  font-weight: 700;
  color: var(--brand-dark);
  min-width: 20px;
}

.med-row .body { flex: 1; min-width: 0; }
.med-row .name { font-weight: 600; word-break: break-word; }
.med-row .dose { font-size: .85rem; color: var(--muted); word-break: break-word; }

.med-row .remove {
  background: none;
  border: none;
  color: var(--danger);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
  min-height: 32px;
}

.empty {
  color: var(--muted);
  font-size: .9rem;
  text-align: center;
  padding: 18px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  margin-bottom: 14px;
}

/* ---- dialog ----------------------------------------------------------- */

dialog {
  border: none;
  border-radius: 14px;
  padding: 0;
  width: min(520px, calc(100vw - 32px));
  box-shadow: 0 12px 40px rgba(0, 0, 0, .22);
}

dialog::backdrop { background: rgba(0, 0, 0, .45); }

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--brand);
  color: #fff;
}

.dialog-head h3 { margin: 0; font-size: 1rem; }

.dialog-body { padding: 16px; max-height: 60vh; overflow-y: auto; }

.dialog-foot {
  display: flex;
  gap: 10px;
  padding: 14px 16px;
  border-top: 1px solid var(--line);
  background: #fafbf8;
  flex-wrap: wrap;
}

.dialog-foot .btn { flex: 1 1 160px; }

/* ---- action bar ------------------------------------------------------- */

.actionbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: flex;
  gap: 10px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, .97);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(6px);
}

.actionbar .btn { flex: 1; }

/* ---- preview ---------------------------------------------------------- */

.preview-frame {
  width: 100%;
  height: min(78vh, 900px);
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #525659;
}

#previewDialog { width: min(860px, calc(100vw - 24px)); }

.status {
  padding: 12px 14px;
  border-radius: 10px;
  font-size: .9rem;
  margin-bottom: 14px;
  display: none;
}

.status.show { display: block; }
.status.error { background: #fdecea; color: var(--danger); border: 1px solid #f3c9c5; }
.status.info { background: var(--brand-light); color: var(--brand-dark); border: 1px solid #dbe5cc; }

/* ---- home ------------------------------------------------------------- */

.home-grid { display: grid; gap: 16px; grid-template-columns: 1fr; }

@media (min-width: 620px) {
  .home-grid { grid-template-columns: 1fr 1fr; }
}

.tile {
  display: block;
  text-decoration: none;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 26px 22px;
  box-shadow: var(--shadow);
}

.tile:hover { border-color: var(--brand); transform: translateY(-2px); }
.tile h2 { margin: 0 0 8px; color: var(--brand-dark); font-size: 1.15rem; }
.tile p { margin: 0; color: var(--muted); font-size: .9rem; }

.brandbar {
  text-align: center;
  padding: 28px 16px 8px;
}

.brandbar h1 { margin: 0; color: var(--brand-dark); font-size: 1.5rem; }
.brandbar p { margin: 6px 0 0; color: var(--muted); font-size: .9rem; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
