:root {
  font-family:
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
  color: #172033;
  background: #f5f7fb;
  --border: #dfe4ec;
  --muted: #687386;
  --surface: #fff;
  --accent: #315efb;
  --danger: #b42318;
}
* {
  box-sizing: border-box;
}
body {
  margin: 0;
  background: #f5f7fb;
}
button,
input,
textarea,
select {
  font: inherit;
}
.app {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 20px;
  margin-bottom: 20px;
}

.in-iframe .topbar,
.in-iframe .tabs {
  display: none;
}
.iframeVisible {
  display: none !important;
}
.in-iframe .iframeVisible {
  display: unset !important;
}
.iframeHidden {
  display: unset !important;
}

.in-iframe .iframeHidden {
  display: none !important;
}

.opennewframe {
  align-self: flex-end !important;
  right: 0;
}
h1,
h2,
h3,
p {
  margin-top: 0;
}
.muted {
  color: var(--muted);
}
.tabs {
  display: flex;
  gap: 6px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 18px;
}
.tab {
  border: 0;
  background: transparent;
  padding: 12px 18px;
  cursor: pointer;
  font-weight: 700;
  color: var(--muted);
  border-bottom: 3px solid transparent;
}
.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}
.hidden {
  display: none !important;
}
.toolbar {
  display: flex !important;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin: 15px 0;
}
.toolbar h2 {
  min-width: 230px;
  text-align: center;
  margin: 0;
}
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  text-decoration: none;
}
.button.secondary {
  background: #fff;
  color: #263248;
  border: 1px solid var(--border);
}
.button.danger {
  background: var(--danger);
}
.calendar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}
.cal-head,
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
}
.cal-head div {
  padding: 10px;
  text-align: center;
  font-weight: 700;
  color: var(--muted);
  background: #fafbfc;
  border-right: 1px solid var(--border);
}
.day {
  min-height: 110px;
  padding: 8px;
  border-top: 1px solid var(--border);
  border-right: 1px solid var(--border);
  background: #fff;
}
.day:nth-child(7n) {
  border-right: 0;
}
.day.other {
  background: #fafafa;
  color: #a0a7b4;
}
.day-number {
  font-weight: 800;
  margin-bottom: 5px;
}
.event-chip {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: #eaf0ff;
  color: #17398c;
  border-radius: 6px;
  padding: 5px 7px;
  margin: 4px 0;
  cursor: pointer;
  font-size: 12px;
}

/* Equipment Grid & Table */
.equipment-grid {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex !important;
}
.eq-table {
  min-width: 820px;
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
}
.eq-table th,
.eq-table td {
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
}
.eq-table th {
  padding: 9px 5px;
  background: #fafbfc;
  font-size: 12px;
}
.eq-name {
  width: 190px;
  min-width: 190px;
  max-width: 190px;
  padding: 10px;
  font-weight: 800;
  background: #fff;
  position: sticky;
  left: 0;
  z-index: 5;
  box-shadow: 2px 0 6px rgba(0, 0, 0, 0.06);
}
.slot {
  width: 48px;
  height: 44px;
  min-width: 48px;
  padding: 0;
  background: #fff;
  cursor: pointer;
  position: relative;
}
.slot:hover {
  background: #f0f4ff;
}
.booking {
  position: absolute;
  inset: 3px 2px;
  background: #dff5e6;
  color: #14532d;
  border-radius: 5px;
  padding: 4px;
  font-size: 11px;
  overflow: hidden;
  cursor: pointer;
  z-index: 3;
}
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 20;
}
.modal-card {
  background: #fff;
  width: min(520px, 100%);
  max-height: 90vh;
  overflow: auto;
  border-radius: 14px;
  padding: 24px;
  position: relative;
}
.close {
  position: absolute;
  right: 15px;
  top: 10px;
  border: 0;
  background: none;
  font-size: 28px;
  cursor: pointer;
}
.field {
  margin: 12px 0;
}
.field label {
  display: block;
  font-weight: 700;
  margin-bottom: 5px;
}
.field input,
.field textarea,
.field select {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 18px;
  flex-wrap: wrap;
}
.notice {
  padding: 12px;
  border-radius: 8px;
  background: #eef4ff;
  margin: 10px 0;
}
.success {
  background: #e9f8ee;
  color: #14532d;
}
.error {
  background: #fff0ef;
  color: #8b1e17;
}

/* Equipment Selector Modal List */
.eq-selector-container {
  max-height: 180px;
  overflow-y: auto;
  border: 1px solid var(--border);
  padding: 8px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.02);
}

label.eq-item-row {
  display: flex !important;
  flex-direction: row !important;
  align-items: center !important;
  justify-content: space-between !important;
  gap: 8px !important;
  padding: 6px !important;
  margin: 0 !important;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  white-space: nowrap !important;
  width: 100% !important;
  box-sizing: border-box !important;
}

label.eq-item-row.unavailable {
  cursor: not-allowed;
  opacity: 0.6;
}

.eq-item-left {
  display: inline-flex !important;
  align-items: center !important;
  gap: 8px !important;
  min-width: 0 !important;
  flex: 1 1 auto !important;
  white-space: nowrap !important;
  overflow: hidden !important;
}

.eq-item-left input[type="checkbox"] {
  width: auto !important;
  height: auto !important;
  flex: 0 0 auto !important;
  margin: 0 !important;
  display: inline-block !important;
}

.eq-item-name {
  display: inline-block !important;
  overflow: hidden !important;
  text-overflow: ellipsis !important;
  white-space: nowrap !important;
  min-width: 0 !important;
}

.eq-item-name.unavailable {
  color: var(--danger);
}

.eq-item-right {
  display: inline-flex !important;
  align-items: center !important;
  gap: 4px !important;
  flex: 0 0 auto !important;
  white-space: nowrap !important;
}

.badge-booked {
  font-size: 0.75rem;
  background: var(--danger);
  color: #fff;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: bold;
}

.badge-category {
  font-size: 0.85rem;
  background: rgba(0, 0, 0, 0.05);
  padding: 2px 6px;
  border-radius: 4px;
}

/* Compact Month Calendar Styles */
.equipment-month-section {
  margin-bottom: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
}

.equipment-month-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.equipment-month-header h3 {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
}

.eq-month-nav {
  display: flex;
  gap: 4px;
}

.compact-btn {
  padding: 4px 8px !important;
  font-size: 12px !important;
  height: 28px;
}

.mini-calendar .cal-head div {
  padding: 4px;
  font-size: 11px;
}

.mini-calendar .day {
  min-height: 48px !important;
  max-height: 58px;
  padding: 3px;
  overflow-y: auto;
}

.mini-calendar .day-number {
  font-size: 10px;
  font-weight: 700;
  margin-bottom: 2px;
}

.mini-calendar .event-chip {
  padding: 2px 4px !important;
  margin: 1px 0 !important;
  font-size: 9px !important;
  line-height: 1.1;
  border-radius: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* --- Mobile Specific Breakpoint Overrides --- */
@media (max-width: 650px) {
  .app {
    padding: 10px;
  }

  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  .topbar h1 {
    font-size: 20px;
  }

  /* Responsive Toolbar Layout */
  .toolbar {
    flex-wrap: wrap;
    gap: 6px;
  }
  .toolbar h2 {
    width: 100%;
    order: -1;
    min-width: unset;
    font-size: 1.1rem;
    margin-bottom: 4px;
  }
  .toolbar .button {
    flex: 1 1 auto;
    padding: 8px 10px;
    font-size: 0.85rem;
  }

  /* Equipment Table Mobile Sizing */
  .eq-table {
    min-width: 580px;
  }
  .eq-name {
    width: 115px;
    min-width: 115px;
    max-width: 115px;
    padding: 8px 6px;
    font-size: 0.8rem;
  }
  .eq-description {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .eq-table th {
    font-size: 11px;
    padding: 6px 2px;
  }
  .slot {
    width: 65px;
    min-width: 65px;
    height: 50px;
  }
  .booking {
    font-size: 10px;
    padding: 3px;
    inset: 2px;
  }

  /* Calendar Mobile Sizing */
  .cal-head div {
    font-size: 11px;
    padding: 7px 2px;
  }
  .day {
    min-height: 80px;
    padding: 4px;
  }
  .event-chip {
    font-size: 10px;
    padding: 4px;
  }

  /* Month Overview Mobile Optimization */
  .equipment-month-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  .eq-month-nav {
    width: 100%;
    justify-content: space-between;
  }
  .mini-calendar .day {
    min-height: 40px !important;
    padding: 2px;
  }

  /* Modal Mobile Sheet Optimization */
  .modal {
    padding: 10px;
  }
  .modal-card {
    padding: 16px;
    max-height: 95vh;
  }
}

/* Equipment Grid Container */
.equipment-grid {
  position: relative;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px;
}

.eq-table {
  min-width: 820px;
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
}

.eq-table th,
.eq-table td {
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  box-sizing: border-box;
}

.eq-table th {
  padding: 9px 5px;
  background: #fafbfc;
  font-size: 12px;
}

/* Fixed Sticky Equipment Column (Desktop) */
.eq-name {
  width: 180px;
  min-width: 180px;
  max-width: 180px;
  padding: 10px;
  font-weight: 800;
  background-color: #ffffff !important; /* Solid background prevents underlying day cells from showing through */
  position: sticky;
  left: 0;
  z-index: 10; /* Ensures equipment column stays strictly above all scrolling day slots */
  box-shadow: 3px 0 6px -2px rgba(0, 0, 0, 0.12); /* Shadow boundary during horizontal scroll */
  background-clip: padding-box;
}

/* Top-Left Corner Header Cell ("Equipment") */
th.eq-name {
  z-index: 11; /* Stacks above both sticky row headers and day columns */
  background-color: #fafbfc !important;
}

/* Day Slots & Booking Chips Z-Index Isolation */
.slot {
  width: 48px;
  height: 44px;
  min-width: 48px;
  padding: 0;
  background: #fff;
  cursor: pointer;
  position: relative;
  z-index: 1; /* Kept below the sticky equipment column (z-index 10) */
}

.slot:hover {
  background: #f0f4ff;
}

.booking {
  position: absolute;
  inset: 3px 2px;
  background: #dff5e6;
  color: #14532d;
  border-radius: 5px;
  padding: 4px;
  font-size: 11px;
  overflow: hidden;
  cursor: pointer;
  z-index: 2; /* Kept below sticky column */
}

/* --- Mobile Specific Overrides (< 650px) --- */
@media (max-width: 650px) {
  .app {
    padding: 10px;
  }

  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  .topbar h1 {
    font-size: 20px;
  }

  .toolbar {
    flex-wrap: wrap;

    gap: 6px;
  }
  .toolbar h2 {
    width: 100%;
    order: -1;
    min-width: unset;
    font-size: 1.1rem;
    margin-bottom: 4px;
  }
  .toolbar .button {
    flex: 1 1 auto;
    padding: 8px 10px;
    font-size: 0.85rem;
  }

  /* Compact Mobile Table & Slim Column */
  .eq-table {
    min-width: 520px; /* Reduced minimum table width for better screen fit */
  }

  .eq-name {
    width: 92px !important; /* Slimmed down from 190px/115px to preserve room for days */
    min-width: 92px !important;
    max-width: 92px !important;
    padding: 6px 4px !important;
    font-size: 0.72rem !important;
    word-break: break-word;
    overflow: hidden;
  }

  .eq-name strong {
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis; /* Truncates long equipment names with '...' */
    font-size: 0.75rem;
  }

  .eq-description {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    font-size: 0.65rem !important;
    line-height: 1.1;
  }

  .eq-table th {
    font-size: 10px;
    padding: 6px 2px;
  }

  .slot {
    width: 60px;
    min-width: 60px;
    height: 48px;
  }

  .booking {
    font-size: 9px;
    padding: 2px;
    inset: 2px;
    line-height: 1.1;
  }

  .cal-head div {
    font-size: 11px;
    padding: 7px 2px;
  }
  .day {
    min-height: 80px;
    padding: 4px;
  }
  .event-chip {
    font-size: 10px;
    padding: 4px;
  }

  .equipment-month-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  .eq-month-nav {
    width: 100%;
    justify-content: space-between;
  }
  .mini-calendar .day {
    min-height: 40px !important;
    padding: 2px;
  }

  .modal {
    padding: 10px;
  }
  .modal-card {
    padding: 16px;
    max-height: 95vh;
  }
}

/* --- Main Calendar Fixes --- */
.calendar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow-x: auto; /* Allows smooth horizontal scrolling on narrow screens */
  -webkit-overflow-scrolling: touch;
}

.cal-head,
.cal-grid {
  display: grid;
  /* Use minmax(0, 1fr) so long text cannot force column widths to expand unequally */
  grid-template-columns: repeat(7, minmax(0, 1fr));
  width: 100%;
  min-width: 350px; /* Prevents extreme crushing on sub-360px viewports */
}

.cal-head div {
  padding: 10px;
  text-align: center;
  font-weight: 700;
  color: var(--muted);
  background: #fafbfc;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  box-sizing: border-box;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.day {
  min-height: 110px;
  min-width: 0; /* Crucial: allows grid child to shrink below content size */
  padding: 8px;
  border-top: 1px solid var(--border);
  border-right: 1px solid var(--border);
  background: #fff;
  overflow: hidden; /* Prevents overflow content from pushing grid borders */
  box-sizing: border-box;
}

.day:nth-child(7n) {
  border-right: 0;
}

.day.other {
  background: #fafafa;
  color: #a0a7b4;
}

.day-number {
  font-weight: 800;
  margin-bottom: 5px;
}

.event-chip {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: #eaf0ff;
  color: #17398c;
  border-radius: 6px;
  padding: 5px 7px;
  margin: 4px 0;
  cursor: pointer;
  font-size: 12px;
  /* Truncate long event titles with '...' to prevent cell width distortion */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  box-sizing: border-box;
}

@media (max-width: 650px) {
  /* Main Calendar Mobile Optimization */
  .cal-head div {
    font-size: 11px;
    padding: 6px 2px;
  }

  .day {
    min-height: 75px; /* Shorter cells on mobile */
    padding: 3px;
  }

  .day-number {
    font-size: 11px;
    margin-bottom: 2px;
  }

  .event-chip {
    font-size: 9px;
    padding: 2px 4px;
    margin: 2px 0;
    border-radius: 4px;
    line-height: 1.2;
  }
}

.event-chip {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: #eaf0ff;
  color: #17398c;
  border-radius: 6px;
  padding: 5px 7px;
  margin: 4px 0;
  cursor: pointer;
  font-size: 12px;
  box-sizing: border-box;

  /* Enable multi-line text wrapping */
  white-space: normal;
  word-break: break-word;
  overflow-wrap: break-word;
  line-height: 1.25;
}

.event-chip {
  font-size: 9px;
  padding: 3px 4px;
  margin: 2px 0;
  border-radius: 4px;
  line-height: 1.15;

  /* Preserve text wrapping on smaller screens */
  white-space: normal;
  word-break: break-word;
  overflow-wrap: break-word;
}

/* Event Rich Text Formatting inside Modal */
.event-description {
  margin-top: 12px;
  line-height: 1.5;
}

.event-description img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  margin: 8px 0;
  display: block;
}

.event-description a {
  color: var(--accent);
  text-decoration: underline;
  word-break: break-all;
}

.event-description ul,
.event-description ol {
  padding-left: 20px;
  margin: 8px 0;
}
