:root {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  color: #111;
  background: #f4f4f4;
  --row-height: 22px;
  --header-height: 30px;
  --grid-height: auto;
  --month-width: minmax(0, 1fr);
  --grid-width: 100%;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--outer-bg, #f4f4f4);
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  gap: 12px;
  padding: 16px 20px 80px;
}

.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
}

.top-bar h1 {
  margin: 0;
  font-size: clamp(20px, 3vw, 28px);
}

.year-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
}

.ghost {
  border: 1px solid rgba(0, 0, 0, 0.2);
  background: transparent;
  padding: 4px 8px;
  border-radius: 999px;
  cursor: pointer;
}

.grid-shell {
  display: block;
  flex: 1;
  min-height: 0;
  height: var(--grid-height);
  overflow: hidden;
}

.grid-scroll-wrapper {
  width: 100%;
  height: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior-x: contain;
  scroll-behavior: smooth;
}

.year-grid {
  display: grid;
  grid-template-columns: repeat(12, var(--month-width));
  grid-template-rows: var(--header-height) repeat(31, var(--row-height));
  border: var(--year-border-width, 2px) solid var(--year-border, #333);
  background: var(--outer-bg, #fff);
  position: relative;
  min-height: 0;
  user-select: none;
  width: var(--grid-width);
  overflow: visible;
}

.month-header {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: clamp(10px, calc(var(--row-height) * 0.5), 14px);
  text-transform: uppercase;
  background: var(--month-header-bg, #fff);
  color: var(--month-header-color, #111);
  border-bottom: 1px solid var(--grid-line, #888);
  letter-spacing: 0.08em;
  scroll-snap-align: start;
}

.day-cell {
  border-bottom: 1px solid var(--grid-line, #888);
  border-right: 1px solid var(--grid-line, #888);
  position: relative;
  overflow: hidden;
}

.day-cell.weekend {
  background: var(--weekend-bg, #fff3c4);
}

.day-cell.weekday {
  background: var(--weekday-bg, #fff);
}

.day-cell.void {
  background: var(--void-bg, #ccc);
  background-image: repeating-linear-gradient(
    45deg,
    rgba(0, 0, 0, 0.2),
    rgba(0, 0, 0, 0.2) 2px,
    transparent 2px,
    transparent 6px
  );
}

.day-number {
  position: absolute;
  top: 2px;
  left: 4px;
  font-size: clamp(9px, calc(var(--row-height) * 0.42), 11px);
  color: var(--day-number, #222);
  pointer-events: none;
  z-index: 1;
}

.event-layer {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
}

.event-strip {
  position: absolute;
  border: 1px solid;
  border-radius: 0;
  padding: 0;
  display: grid;
  align-items: center;
  justify-items: var(--event-align, center);
  overflow: hidden;
  pointer-events: auto;
  opacity: var(--event-opacity, 1);
  transition: opacity 150ms ease-in-out;
  cursor: pointer;
}

.event-strip.is-filtered {
  opacity: 0;
  pointer-events: none;
}

.ghost-strip {
  position: absolute;
  border: 1px dashed rgba(0, 0, 0, 0.5);
  background: rgba(255, 255, 255, 0.5);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.6);
  border-radius: 0;
  pointer-events: none;
  transition: opacity 150ms ease-in-out;
  z-index: 4;
}

.event-strip.vertical {
  writing-mode: vertical-lr;
  text-orientation: mixed;
}

.event-strip.horizontal {
  writing-mode: horizontal-tb;
}

.event-strip.open-top {
  border-top-width: 0;
}

.event-strip.open-bottom {
  border-bottom-width: 0;
}

.dock {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  display: flex;
  gap: 12px;
  padding: 10px 18px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.75);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.dock-btn {
  border: none;
  background: transparent;
  font-size: clamp(16px, 2.5vw, 20px);
  cursor: pointer;
}

.popover {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  min-width: 220px;
  padding: 16px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(20px);
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.popover h3 {
  margin-top: 0;
}

.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.45);
  z-index: 10;
}

.modal-card {
  background: rgba(255, 255, 255, 0.92);
  padding: 24px;
  border-radius: 20px;
  width: min(360px, 90vw);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  max-height: min(90vh, 680px);
  overflow-y: auto;
}

.modal-card.modal-wide {
  width: min(760px, 95vw);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.modal-card h2 {
  margin-top: 0;
}

.modal-card form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-card input,
.modal-card select,
.modal-card textarea {
  width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.2);
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.form-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.fabrics-layout {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.swatch-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 10px;
}

.swatch {
  border: 1px solid rgba(0, 0, 0, 0.2);
  border-radius: 16px;
  padding: 8px;
  text-align: center;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 11px;
  font-weight: 600;
}

.swatch-chip {
  height: 24px;
  border-radius: 999px;
  border: 1px solid rgba(0, 0, 0, 0.3);
}

.category-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 240px;
  overflow-y: auto;
}

.category-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 10px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  background: rgba(255, 255, 255, 0.6);
  font-size: 12px;
}

.category-meta {
  font-size: 12px;
  color: #555;
}

.category-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 60px;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid rgba(0, 0, 0, 0.3);
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.category-chip.is-preview {
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.12);
}

.categories-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.category-manager-actions {
  margin-bottom: 12px;
}

.category-create {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.category-table {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 240px;
  overflow-y: auto;
  margin-bottom: 16px;
}

.category-row {
  display: grid;
  grid-template-columns: 1.2fr 1fr 0.6fr 0.6fr 0.6fr auto;
  gap: 6px;
  align-items: center;
  padding: 6px 8px;
  border-radius: 10px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  background: rgba(255, 255, 255, 0.6);
  font-size: 12px;
}

.category-row input,
.category-row select {
  width: 100%;
}

.event-editor {
  position: fixed;
  z-index: 12;
  width: min(360px, 92vw);
  padding: 16px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(20px);
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  max-height: 85vh;
  overflow-y: auto;
}

.event-editor form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.lane-controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.lane-control {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.control-label {
  font-size: 12px;
  color: #555;
}

.control-buttons {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 6px;
  border-radius: 12px;
  border: 1px solid rgba(0, 0, 0, 0.15);
  background: rgba(255, 255, 255, 0.7);
}

.control-value {
  min-width: 32px;
  text-align: center;
  font-weight: 600;
}

.icon-btn {
  border: 1px solid rgba(0, 0, 0, 0.2);
  background: transparent;
  padding: 4px 8px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
}

.editor-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.helper-text {
  font-size: 12px;
  color: #666;
  margin: 0;
}

.settings-actions {
  margin-top: 12px;
}

.settings-section {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid rgba(0, 0, 0, 0.08);
}

.settings-section h3 {
  margin: 0 0 8px;
  font-size: 14px;
}

.settings-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}

.settings-export {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.settings-export button {
  width: 100%;
}

.import-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.danger {
  color: #b00020;
  border-color: rgba(176, 0, 32, 0.4);
}

.error {
  color: #b00020;
}

@media (max-width: 900px) {
  #app {
    padding: 12px 12px 80px;
  }

  .grid-scroll-wrapper {
    scroll-snap-type: none;
  }

  .fabrics-layout {
    grid-template-columns: 1fr;
  }

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

@media (max-width: 720px) {
  .modal-card {
    padding: 16px;
    width: min(360px, 94vw);
  }

  .modal-card.modal-wide {
    width: min(520px, 96vw);
  }

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

  .category-create {
    grid-template-columns: 1fr;
  }

  .settings-export {
    grid-template-columns: 1fr;
  }
}

body.portrait {
  overflow: hidden;
}

body.allow-vertical-scroll {
  overflow-y: auto;
}

body.portrait .year-grid {
  scroll-snap-type: none;
}

body.portrait .grid-scroll-wrapper {
  overscroll-behavior-x: contain;
}

body.portrait .grid-shell {
  overflow: hidden;
}
