:root {
  --nw-purple: #4E2A84;
  --bg: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --reserved: #e5e7ff;
  --accent: #4E2A84;
  --header-height: 56px;
  --slot-height: 28px;
  --time-col-width: 72px;
  --header-row-height: 44px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  /* Force full viewport height */
  overflow: hidden;
  /* Prevent body scroll, let children scroll */
  display: flex;
  /* Use flexbox to stack header and calendar */
  flex-direction: column;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, sans-serif;
  color: var(--text);
  background: var(--bg);
}

/* App Header */
.app-header {
  position: relative;
  /* No longer sticky, just part of flex flow */
  flex-shrink: 0;
  /* Never shrink */
  z-index: 100;
  background: #fff;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  flex-wrap: wrap;
  gap: 8px;
  width: 100%;
  height: var(--header-height);
  box-sizing: border-box;
}

.app-header h1 {
  font-size: 18px;
  margin: 0;
  flex-shrink: 0;
}

.controls {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: nowrap;
}

.controls button,
.controls input[type="date"] {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font-size: 14px;
}

.controls input[type="date"] {
  width: 140px;
  flex: 0 0 140px;
}

.controls button#todayBtn {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}

/* Calendar Container */
.calendar-container {
  position: relative;
  width: 100%;
  max-width: 100vw;
  flex: 1;
  /* Take up remaining vertical space */
  min-height: 0;
  /* Allow flex child to shrink properly */
  overflow-x: hidden;
  overflow-y: auto;
  /* Scroll strictly within this container */
  /* height: calc(100vh - var(--header-height)); REMOVED */
  -webkit-overflow-scrolling: touch;
  margin: 0;
  padding: 0;
}

.calendar {
  display: table;
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
  margin: 0;
  padding: 0;
}

/* Time Column */
.time-col {
  display: table-cell;
  width: var(--time-col-width);
  vertical-align: top;
  position: sticky;
  left: 0;
  z-index: 15;
  background: #fff;
  border-right: 1px solid var(--border);
  padding: 0;
  margin: 0;
}

.time-col-header {
  height: var(--header-row-height);
  min-height: var(--header-row-height);
  max-height: var(--header-row-height);
  background: #fff;
  position: sticky;
  top: 0;
  z-index: 21;
  border-bottom: 1px solid var(--border);
  border-top: 1px solid var(--border);
  margin: 0;
  padding: 0;
}

.time-col-slots {
  display: grid;
  grid-template-rows: repeat(48, var(--slot-height));
  position: relative;
}

.time-col .time {
  height: var(--slot-height);
  font-size: 11px;
  color: var(--muted);
  border-top: 1px dashed var(--border);
  padding: 0 4px;
  /* Removed line-height and aligned items to start to position text at grid line */
  display: flex;
  align-items: flex-start;
}

.time-col .time:first-child {
  border-top: none;
}

/* Full hours (odd-numbered slots: 1st, 3rd, 5th...) get solid borders */
.time-col .time:nth-child(odd) {
  border-top: 1px solid var(--border);
}

.time-col .time-text {
  display: block;
  transform: translateY(-50%);
  background: var(--bg);
  padding-right: 2px;
}

.time-col .time:first-child .time-text {
  transform: translateY(0);
}

/* Calendar Right Side */
.calendar-right {
  display: table-cell;
  vertical-align: top;
  width: auto;
  min-width: 0;
  overflow: visible;
  padding: 0;
  margin: 0;
  position: relative;
}

.printers-wrapper {
  width: 100%;
  display: flex;
  flex-direction: column;
  margin: 0;
  padding: 0;
  overflow: visible;
}

/* Printer Headers */
.printers-header {
  display: table;
  width: 100%;
  table-layout: fixed;
  border-collapse: separate;
  border-spacing: 0;
  position: sticky;
  top: 0;
  z-index: 20;
  background: #fff;
  margin: 0;
  padding: 0;
  flex-shrink: 0;
  will-change: transform;
}

.printers-header .printer-header {
  display: table-cell;
  height: var(--header-row-height);
  min-height: var(--header-row-height);
  max-height: var(--header-row-height);
  padding: 4px;
  background: #fafafa;
  border: 1px solid var(--border);
  border-top: 1px solid var(--border);
  border-left-width: 0;
  vertical-align: middle;
  white-space: normal;
  overflow: hidden;
  text-overflow: ellipsis;
  word-wrap: break-word;
  box-sizing: border-box;
  position: relative;
}

.printers-header .printer-header:first-child {
  border-left: 1px solid var(--border);
}

.printers-header .printer-header .printer-name {
  font-weight: 600;
  color: var(--nw-purple);
  font-size: 14px;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
}

.printers-header .printer-header .printer-type {
  font-size: 11px;
  color: var(--muted);
  line-height: 1.2;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Printer Columns */
.printers {
  display: table;
  width: 100%;
  table-layout: fixed;
  border-collapse: separate;
  border-spacing: 0;
  margin: 0;
  padding: 0;
  position: relative;
}

.printer-col {
  display: table-cell;
  border: 1px solid var(--border);
  border-top: none;
  border-left-width: 0;
  vertical-align: top;
  background: #fff;
  margin: 0;
  padding: 0;
}

.printer-col:first-child {
  border-left: 1px solid var(--border);
}

.printer-col .slots {
  display: grid;
  grid-template-rows: repeat(48, var(--slot-height));
  position: relative;
}

.slot {
  height: var(--slot-height);
  border-top: 1px dashed var(--border);
  box-sizing: border-box;
}

.slot:first-child {
  border-top: none;
}

/* Full hours (odd-numbered slots: 1st, 3rd, 5th...) get solid borders */
.slot:nth-child(odd) {
  border-top: 1px solid var(--border);
}

.slot:hover {
  background: #fafafa;
  cursor: pointer;
}

/* Reservation Blocks */
.block {
  position: absolute;
  left: 6px;
  right: 6px;
  border-radius: 6px;
  background: var(--reserved);
  border: 1px solid #c7c9ff;
  color: #3730a3;
  font-size: 12px;
  padding: 4px 6px;
  display: flex;
  align-items: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  box-sizing: border-box;
}

.selection {
  position: absolute;
  left: 6px;
  right: 6px;
  background: rgba(78, 42, 132, 0.12);
  border: 1px solid var(--nw-purple);
  border-radius: 6px;
  box-sizing: border-box;
}

.selection .handle {
  position: absolute;
  left: 0;
  right: 0;
  height: 10px;
  cursor: ns-resize;
  pointer-events: auto;
  touch-action: none;
  z-index: 20;
}

.selection .handle.top {
  top: -6px;
}

.selection .handle.bottom {
  bottom: -6px;
}

.selection .handle::after {
  content: "";
  display: block;
  width: 40px;
  height: 4px;
  background: var(--nw-purple);
  opacity: 0.6;
  margin: 3px auto;
  border-radius: 4px;
}

/* Reserve Button */
.selection .reserve-btn {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 8px 16px;
  min-height: 44px;
  background: var(--accent);
  color: #fff;
  border: 1px solid var(--accent);
  border-radius: 6px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  z-index: 25;
  white-space: nowrap;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: background-color 0.2s, transform 0.1s;
  pointer-events: auto;
  touch-action: manipulation;
}

.selection .reserve-btn:hover {
  background: #3d1f6b;
  transform: translate(-50%, -50%) scale(1.05);
}

.selection .reserve-btn:active {
  transform: translate(-50%, -50%) scale(0.98);
}

/* Ensure button is visible even for small selections */
.selection[style*="height: 28px"] .reserve-btn,
.selection[style*="height: 26px"] .reserve-btn {
  padding: 4px 12px;
  min-height: auto;
  font-size: 12px;
}

/* Current Time Indicator */
.current-time {
  position: absolute;
  left: 0;
  right: 0;
  height: 2px;
  background: #dc2626;
  z-index: 10;
  pointer-events: none;
}

/* Loading */
.loading {
  display: none;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 30;
  text-align: center;
  background: rgba(255, 255, 255, 0.95);
  padding: 24px 32px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.loading.active {
  display: block;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 12px;
}

.loading-text {
  color: var(--muted);
  font-size: 14px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Dialog */
dialog {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0;
  width: min(560px, 92vw);
  max-height: 92vh;
}

dialog[open] {
  display: flex;
  flex-direction: column;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.2);
}

form#reservationForm {
  padding: 0;
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.form-body {
  padding: 16px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

form h2 {
  margin: 0 0 12px 0;
  font-size: 18px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
  min-width: 0;
  /* Prevent grid blowout */
}

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

label {
  font-size: 13px;
  color: var(--muted);
}

input,
select {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  width: 100%;
  max-width: 100%;
  /* Ensure input doesn't exceed container */
  min-width: 0;
  /* Allow flex item to shrink below content size */
  box-sizing: border-box;
  -webkit-appearance: none;
  /* remove iOS default styles */
  appearance: none;
  background-color: #fff;
  /* ensure background is white after appearance removal */
  color: var(--text);
  /* Force text color to match theme (overriding iOS blue default) */
}

/* Restore arrow for selects since appearance:none removes it */
select {
  background-image: url("data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%231f2937%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E");
  background-repeat: no-repeat;
  background-position: right .7em top 50%;
  background-size: .65em auto;
  padding-right: 1.5em;
}

.input-with-btn {
  display: flex;
  gap: 6px;
  align-items: stretch;
  min-width: 0;
}

.input-with-btn input {
  flex: 1;
  min-width: 0;
}

.input-with-btn button {
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  width: 100%;
  box-sizing: border-box;
  background: #fff;
  border-top: 1px solid var(--border);
  margin: 0;
  padding: 12px 16px;
  flex-shrink: 0;
}

.dialog-actions button {
  flex: 1 1 0;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  box-sizing: border-box;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 1.1rem;
}

#reserveBtn {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

#reserveBtn:disabled {
  background: #ccc;
  border-color: #ccc;
  color: #666;
  cursor: not-allowed;
}

#cancelBtn {
  color: #e53935;
  border-color: #e53935;
}

.checkbox-field .checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background-color: #fff;
  font-size: 14px;
  color: var(--text);
  line-height: 1.4;
  cursor: pointer;
}

.checkbox-field input[type="checkbox"] {
  -webkit-appearance: checkbox;
  appearance: checkbox;
  width: auto;
  min-width: 0;
  padding: 0;
  border: none;
  border-radius: 0;
  background: none;
  flex-shrink: 0;
  margin: 0;
  cursor: pointer;
}

.error {
  color: #991b1b;
  background-color: #fee2e2;
  border: 1px solid #ef4444;
  border-radius: 8px;
  padding: 12px;
  margin-top: 12px;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: auto;
  text-align: center;
}

.error:empty {
  display: none;
}

.conflict-status {
  padding: 8px 10px;
  border: 1px solid transparent;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  min-height: 38px;
  /* Match input height roughly */
  box-sizing: border-box;
  margin-top: 4px;
}

.conflict-status.success {
  color: #166534;
  background-color: #dcfce7;
  border-color: #166534;
}

.conflict-status.error {
  color: #991b1b;
  background-color: #fee2e2;
  border-color: #ef4444;
}

/* Mobile Responsive */
@media (max-width:720px) {
  :root {
    --time-col-width: 56px;
  }

  .app-header {
    flex-direction: column;
    align-items: stretch;
    height: auto;
    min-height: var(--header-height);
  }

  .app-header h1 {
    font-size: 16px;
  }

  .controls {
    width: 100%;
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .controls button#prevDay,
  .controls button#nextDay {
    flex: 0 0 auto;
    min-width: 36px;
    padding: 6px 8px;
    font-size: 16px;
  }

  .controls button#todayBtn {
    flex: 0 1 auto;
    min-width: 60px;
    font-size: 13px;
    padding: 6px 10px;
  }

  .controls input[type="date"] {
    width: 140px;
    flex: 0 0 140px;
    font-size: 13px;
    padding: 6px 10px;
  }

  .printers-header .printer-header {
    padding: 6px 4px;
  }

  .printers-header .printer-header .printer-name {
    font-size: 12px;
  }

  .printers-header .printer-header .printer-type {
    font-size: 10px;
  }

  .block {
    font-size: 11px;
    padding: 3px 4px;
  }

  .row {
    grid-template-columns: 1fr;
  }

  /* Fix dialog buttons on mobile */
  dialog {
    width: 100%;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    border-radius: 0;
    border: none;
    margin: 0;
  }

  /* Ensure inputs have 16px font size to prevent iOS zoom */
  input,
  select,
  textarea {
    font-size: 16px !important;
  }

  .dialog-actions {
    flex-wrap: nowrap;
    /* Force single row */
    gap: 12px;
  }

  .dialog-actions button {
    flex: 1 1 0;
    /* Grow, shrink, and start from 0 basis to ensure equality */
    width: 100%;
    min-width: 0;
    /* Allow shrinking if needed */
    max-width: none;
  }

  /* Larger touch targets for resize handles on mobile */
  .selection .handle {
    height: 44px;
    min-height: 44px;
    left: 50%;
    right: auto;
    width: 80px;
    margin-left: -40px;
  }

  .selection .handle.top {
    top: -22px;
  }

  .selection .handle.bottom {
    bottom: -22px;
  }

  .selection .handle::after {
    width: 50px;
    height: 5px;
    margin: 19.5px auto;
  }

  /* Smaller Reserve button on mobile */
  .selection .reserve-btn {
    padding: 6px 12px;
    min-height: 36px;
    font-size: 13px;
  }
}

@media (max-width:480px) {
  :root {
    --time-col-width: 48px;
    --slot-height: 26px;
  }

  .printers-header .printer-header {
    padding: 4px 2px;
  }

  .printers-header .printer-header .printer-name {
    font-size: 11px;
  }

  .printers-header .printer-header .printer-type {
    font-size: 9px;
  }

  .time-col .time {
    font-size: 10px;
  }
}

/* Floating action button */
.fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 50;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 28px;
  padding: 0 24px;
  height: 56px;
  font-size: 1rem;
  font-weight: 400;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(78, 42, 132, 0.35);
  display: flex;
  align-items: center;
  gap: 8px;
  transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
  user-select: none;
}
.fab:hover {
  background: #3d2169;
  box-shadow: 0 6px 16px rgba(78, 42, 132, 0.45);
}
.fab:active {
  transform: scale(0.97);
}
.fab-icon {
  flex-shrink: 0;
}

@media (max-width: 480px) {
  .fab {
    bottom: 20px;
    right: 16px;
    height: 52px;
    width: 52px;
    padding: 0;
    border-radius: 50%;
    justify-content: center;
  }
  .fab-label {
    display: none;
  }
}