:root {
  --ink: #17202a;
  --paper: #ffffff;
  --line: #7a828a;
  --line-light: #d7dde2;
  --mark: #146c43;
  --mark-box: #6e6e6e;
  --mark-soft: #e7f3ec;
  --night: #f2f4f7;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: Arial, Helvetica, sans-serif;
  font-size: 18px;
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
}

.sheet {
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px calc(16px + env(safe-area-inset-right)) calc(40px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
}

.sheet-header h1 {
  margin: 0 0 4px;
  font-size: 34px;
  letter-spacing: -0.01em;
}

.name-line {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin: 10px 0 16px;
  font-size: 20px;
  font-weight: 700;
}

.name-line input {
  flex: 0 1 360px;
  min-height: 44px;
  border: none;
  border-bottom: 2px solid var(--ink);
  border-radius: 0;
  padding: 4px 8px;
  background: transparent;
  font: inherit;
  font-size: 18px;
  font-weight: 400;
  color: var(--ink);
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.copy-status {
  font-size: 15px;
  font-weight: 700;
  color: var(--mark);
}

/* Clock format switch */
.time-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
  font-size: 15px;
}

.clock-label { color: #4b5560; }
.clock-label.is-active { color: var(--ink); font-weight: 700; }

#clockToggle {
  position: relative;
  width: 56px;
  height: 32px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: var(--paper);
  cursor: pointer;
  padding: 0;
  flex: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

#clockToggle .knob {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--ink);
  transition: left 0.15s ease;
}

#clockToggle[aria-checked="true"] .knob { left: 27px; }

#clockToggle:focus-visible {
  outline: 3px solid var(--mark);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  #clockToggle .knob { transition: none; }
}

/* Stacked 12-hour labels ("1:00" over "am") in grid columns */
.hour-label .ampm,
.hour-btn .ampm {
  display: block;
  font-size: 10px;
  font-weight: 400;
  line-height: 1.1;
}

body.clock12 .hour-label {
  font-size: 11px;
  padding: 4px 1px;
  min-width: 36px;
}

body.clock12 .med-col { min-width: 150px; }
body.clock12 .med-name { min-width: 140px; }

body.clock12 .hour-btn { font-size: 13px; }
body.clock12 .hour-btn .ampm { font-size: 9px; }

.button {
  font: inherit;
  font-weight: 700;
  padding: 10px 18px;
  border: 2px solid var(--ink);
  background: var(--paper);
  color: var(--ink);
  border-radius: 6px;
  cursor: pointer;
  min-height: 48px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.button:hover { background: var(--mark-soft); }

@media (pointer: coarse) {
  .button:active { background: var(--line-light); }
}

.button:focus-visible,
.slot:focus-visible,
.med-name:focus-visible,
.table-scroll:focus-visible {
  outline: 3px solid var(--mark);
  outline-offset: 2px;
}

.table-scroll { overflow-x: auto; }

.time-sheet {
  /* border-collapse: separate keeps cell borders attached to the sticky
     medicine column while the hour grid scrolls beneath it (Safari). */
  border-collapse: separate;
  border-spacing: 0;
  border-top: 1px solid var(--line);
  border-left: 1px solid var(--line);
  width: 100%;
  min-width: 940px;
}

.time-sheet caption {
  caption-side: top;
  text-align: left;
  font-size: 15px;
  color: #4b5560;
  padding-bottom: 6px;
}

.time-sheet th,
.time-sheet td {
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  text-align: center;
  padding: 0;
}

.day-part {
  font-size: 15px;
  padding: 6px 2px;
  background: var(--night);
}


.hour-label {
  font-size: 13px;
  font-weight: 700;
  padding: 6px 0;
  min-width: 34px;
  background: var(--night);
}

.med-col {
  text-align: left;
  padding: 6px 8px;
  min-width: 180px;
  background: var(--night);
  font-size: 16px;
}

.med-cell {
  text-align: left;
  padding: 0;
  background: var(--paper);
}

.med-col,
.med-cell {
  position: sticky;
  left: 0;
  z-index: 2;
}

.med-name {
  font: inherit;
  font-size: 17px;
  width: 100%;
  min-width: 170px;
  border: none;
  padding: 12px 8px;
  background: transparent;
  color: var(--ink);
}

.slot {
  display: block;
  width: 100%;
  height: 48px;
  min-width: 34px;
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 22px;
  line-height: 1;
  color: transparent;
  padding: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.slot:hover { background: var(--mark-soft); }

.slot[aria-pressed="true"] {
  background: var(--mark-box);
  color: transparent;
}

.slot[aria-pressed="true"]:hover { background: #5c5c5c; }

.hint {
  margin-top: 14px;
  font-size: 15px;
  color: #4b5560;
}

.scroll-hint {
  display: none;
  margin: 0 0 8px;
  font-size: 15px;
  font-weight: 700;
  color: #4b5560;
}

.hours-note {
  margin: 0 0 10px;
  font-size: 15px;
  color: #4b5560;
}

@media (min-width: 701px) and (max-width: 980px) {
  .scroll-hint:not([hidden]) { display: block; }
}

/* Touch devices: larger tap targets and a narrower sticky medicine
   column so more hour boxes stay visible when the table layout is used. */
@media (pointer: coarse) {
  .slot { height: 54px; min-width: 42px; }
  .hour-label { min-width: 42px; padding: 8px 0; font-size: 14px; }
  .med-col { min-width: 132px; }
  .med-name { min-width: 122px; font-size: 16px; }
}

/* Phone layout: each medicine is a card with a 24-hour tap keypad,
   grouped into night/morning/afternoon/evening rows. No sideways scroll. */
.card-list {
  display: grid;
  gap: 14px;
}

.med-card {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  background: var(--paper);
}

.med-card .med-name {
  min-width: 0;
  padding: 8px 4px 10px;
  margin-bottom: 10px;
  border-bottom: 2px solid var(--line-light);
  font-size: 18px;
  font-weight: 700;
}

.hour-grid {
  display: grid;
  grid-template-columns: minmax(52px, auto) repeat(6, minmax(0, 1fr));
  gap: 6px;
  align-items: stretch;
}

.part-label {
  align-self: center;
  font-size: 12px;
  font-weight: 700;
  color: #4b5560;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.hour-btn {
  min-height: 46px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: #4b5560;
  cursor: pointer;
  padding: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.hour-btn:active { background: var(--mark-soft); }

.hour-btn[aria-pressed="true"] {
  background: var(--mark-box);
  border-color: var(--mark-box);
  color: #ffffff;
}

/* Add / remove medicines */
.row-actions { margin-top: 12px; }

.row-actions .button:disabled {
  cursor: default;
  opacity: 0.55;
  background: var(--paper);
}

.remove-head { background: var(--night); min-width: 44px; }

.remove-cell { background: var(--paper); }

.remove-med {
  display: block;
  width: 100%;
  height: 48px;
  min-width: 44px;
  border: none;
  background: transparent;
  color: #9c2f24;
  font: inherit;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.remove-med:hover:not(:disabled) { background: #fff2f0; }
.remove-med:disabled { color: var(--off, #bfbfbf); opacity: 0.4; cursor: default; }

.card-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  border-bottom: 2px solid var(--line-light);
}

.card-head .med-name {
  flex: 1;
  margin-bottom: 0;
  border-bottom: none;
}

.remove-card {
  flex: none;
  min-height: 44px;
  padding: 6px 12px;
  border: 1px solid #9c2f24;
  border-radius: 8px;
  background: var(--paper);
  color: #9c2f24;
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.remove-card:disabled { opacity: 0.4; cursor: default; }

.sheet-footer {
  margin-top: 24px;
  border-top: 2px solid var(--line-light);
  padding-top: 12px;
  font-size: 15px;
  color: #4b5560;
}

.sheet-footer a { color: var(--mark); }

.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;
}

@media print {
  @page { size: letter landscape; margin: 10mm; }
  body { font-size: 11px; }
  .toolbar, .hint, noscript, .sheet-footer, .card-list, .scroll-hint,
  .hours-note, .row-actions, .remove-head, .remove-cell, .time-toggle { display: none !important; }
  .table-scroll { display: block !important; overflow: visible; }
  .sheet { padding: 0; max-width: none; }
  .sheet-header h1 { font-size: 22px; }
  .name-line { font-size: 14px; margin: 6px 0 10px; }
  .name-line input {
    min-height: 0;
    flex-basis: 300px;
    padding: 2px 6px;
    border-bottom: 1.5px solid #000;
    font-size: 13px;
  }
  /* Fixed-percentage layout: the medicine column takes 14% and the 24
     hour columns share the rest equally, so the grid always fits one
     page width regardless of paper size or orientation. */
  .time-sheet { min-width: 0; width: 100%; table-layout: fixed; }
  .med-col, .med-cell { position: static; min-width: 0; width: 14%; box-shadow: none; }
  .hour-label { min-width: 0; padding: 3px 0; font-size: 9px; }
  .hour-label .ampm { font-size: 7px; }
  .day-part { padding: 3px 1px; font-size: 10px; }
  .med-name { min-width: 0; width: 100%; font-size: 11px; padding: 5px 3px; }
  .slot { height: 22px; min-width: 0; font-size: 13px; }
  /* Borders always print, even with "background graphics" disabled, so
     marked cells are filled with a thick border rather than relying on
     the background color alone. */
  .slot[aria-pressed="true"] {
    border: 11px solid var(--mark-box);
    background: var(--mark-box);
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }
  .time-sheet caption { font-size: 10px; }
  .sheet-footer { font-size: 10px; margin-top: 10px; }
  .time-sheet th, .time-sheet td { border-color: #000; }
}
