* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #1a1a2e;
  --bg-panel: #16213e;
  --bg-card: #1a1a3e;
  --bg-card-hover: #222250;
  --accent: #e94560;
  --accent-dim: #a83248;
  --text: #eee;
  --text-dim: #8892b0;
  --border: #2a2a4a;
  --green: #4ade80;
  --yellow: #fbbf24;
  --red: #ef4444;
  --highlight: rgba(255, 255, 255, 0.08);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  /* Safe area for notched iPhones */
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
}

header h1 {
  font-size: 1.4rem;
  font-weight: 700;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 12px;
}

.toolbar-actions {
  display: flex;
  gap: 6px;
}

.toolbar-btn {
  padding: 8px 16px;
  background: var(--bg-card);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.88rem;
  font-weight: 600;
  transition: all 0.2s;
  white-space: nowrap;
}

.toolbar-btn:hover {
  background: var(--bg-card-hover);
  color: var(--text);
  border-color: var(--accent);
}

/* Shared banner */
.shared-banner {
  background: linear-gradient(90deg, #1e3a5f, #16213e);
  color: var(--text-dim);
  text-align: center;
  padding: 8px 16px;
  font-size: 0.85rem;
  border-bottom: 1px solid var(--border);
  position: relative;
}

.banner-dismiss {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 4px 8px;
}

.banner-dismiss:hover {
  color: var(--text);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: var(--bg-panel);
  color: var(--text);
  border: 1px solid var(--accent);
  padding: 12px 24px;
  border-radius: 10px;
  font-size: 0.92rem;
  font-weight: 600;
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: transform 0.35s ease, opacity 0.35s ease;
  box-shadow: 0 4px 20px rgba(233, 69, 96, 0.25);
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.container-toggle {
  display: flex;
  gap: 0;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.toggle-btn {
  padding: 8px 20px;
  background: transparent;
  color: var(--text-dim);
  border: none;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 600;
  transition: all 0.2s;
}

.toggle-btn.active {
  background: var(--accent);
  color: white;
}

.toggle-btn:hover:not(.active) {
  background: var(--bg-card);
}

/* === MAIN LAYOUT — left panel 45% === */
.main-layout {
  display: flex;
  height: calc(100vh - 65px);
  height: calc(100dvh - 65px); /* Safari mobile safe viewport */
  overflow: hidden;
}

.panel-left {
  width: 45%;
  min-width: 400px;
  max-width: 700px;
  padding: 16px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex-shrink: 0;
}

.panel-right {
  flex: 1;
  position: relative;
  background: #0f0f23;
  min-width: 0; /* prevent flex overflow */
  min-height: 0;
}

#three-container {
  width: 100%;
  height: 100%;
  display: block; /* prevent inline gaps */
}

/* Overflow warning */
.overflow-warning {
  display: none;
  position: absolute;
  top: 16px;
  left: 16px;
  background: rgba(239, 68, 68, 0.9);
  color: white;
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 0.88rem;
  font-weight: 600;
  z-index: 10;
  backdrop-filter: blur(8px);
}

.view-controls {
  position: absolute;
  bottom: 16px;
  right: 16px;
  display: flex;
  gap: 8px;
}

.view-controls button {
  padding: 8px 14px;
  background: rgba(26, 26, 46, 0.85);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.82rem;
  backdrop-filter: blur(8px);
  transition: all 0.2s;
}

.view-controls button:hover {
  background: var(--bg-card);
  color: var(--text);
}

/* Stats */
.stats-bar {
  display: flex;
  gap: 12px;
}

.stat {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  text-align: center;
}

.stat-label {
  display: block;
  font-size: 0.72rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 4px;
}

.stat-value {
  font-size: 1.1rem;
  font-weight: 700;
}

/* Fill bar */
.fill-bar-container {
  position: relative;
  height: 28px;
  background: var(--bg-card);
  border-radius: 8px;
  border: 1px solid var(--border);
  overflow: hidden;
}

.fill-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--green), var(--green));
  border-radius: 7px;
  transition: width 0.4s ease, background 0.4s ease;
}

.fill-bar.warning {
  background: linear-gradient(90deg, var(--yellow), var(--yellow));
}

.fill-bar.danger {
  background: linear-gradient(90deg, var(--red), var(--red));
}

.fill-label {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 0.85rem;
  font-weight: 700;
  text-shadow: 0 1px 3px rgba(0,0,0,0.5);
}

/* Rooms */
.rooms-container {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0; /* critical for flex children to scroll */
}

.room-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

.room-header {
  display: flex;
  align-items: center;
  padding: 10px 14px;
  cursor: pointer;
  gap: 10px;
  transition: background 0.15s;
}

.room-header:hover {
  background: var(--bg-card-hover);
}

.room-color-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
}

.room-name {
  flex: 1;
  font-weight: 600;
  font-size: 0.95rem;
}

.room-volume {
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-right: 8px;
}

.room-chevron {
  color: var(--text-dim);
  font-size: 0.8rem;
  transition: transform 0.2s;
}

.room-chevron.open {
  transform: rotate(90deg);
}

.room-actions {
  display: flex;
  gap: 4px;
}

.btn-icon {
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  border-radius: 4px;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}

.btn-icon:hover {
  background: rgba(255,255,255,0.08);
  color: var(--text);
}

.btn-icon.delete:hover {
  color: var(--red);
}

.room-items {
  display: none;
  padding: 0 14px 10px;
}

.room-items.open {
  display: block;
}

/* === ITEM ROW with qty buttons and highlight === */
.item-row {
  display: flex;
  align-items: center;
  padding: 6px 8px;
  margin: 0 -8px;
  border-top: 1px solid rgba(255,255,255,0.04);
  gap: 8px;
  font-size: 0.88rem;
  border-radius: 6px;
  transition: background 0.15s, box-shadow 0.15s;
  cursor: default;
}

.item-row:hover {
  background: var(--highlight);
}

.item-row.highlighted {
  background: rgba(233, 69, 96, 0.15);
  box-shadow: inset 0 0 0 1px rgba(233, 69, 96, 0.4);
}

.item-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.item-name.editable,
.item-dims.editable {
  cursor: pointer;
  border-bottom: 1px dashed transparent;
  transition: border-color 0.15s;
}

.item-name.editable:hover,
.item-dims.editable:hover {
  border-bottom-color: var(--accent);
}

/* Inline edit inputs */
.inline-edit-input {
  background: var(--bg);
  border: 1px solid var(--accent);
  border-radius: 4px;
  color: var(--text);
  font-size: 0.88rem;
  padding: 2px 6px;
  outline: none;
  flex: 1;
  min-width: 0;
}

.inline-dims-edit {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}

.inline-dim-input {
  width: 42px;
  background: var(--bg);
  border: 1px solid var(--accent);
  border-radius: 4px;
  color: var(--text);
  font-size: 0.78rem;
  padding: 2px 4px;
  text-align: center;
  outline: none;
  -moz-appearance: textfield;
}

.inline-dim-input::-webkit-inner-spin-button,
.inline-dim-input::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.inline-dim-sep {
  color: var(--text-dim);
  font-size: 0.72rem;
}

.item-dims {
  color: var(--text-dim);
  font-size: 0.78rem;
  white-space: nowrap;
}

.item-vol {
  color: var(--text-dim);
  font-size: 0.78rem;
  white-space: nowrap;
  min-width: 55px;
  text-align: right;
}

/* === QTY CONTROLS — inline +/- buttons === */
.qty-controls {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}

.qty-btn {
  width: 22px;
  height: 22px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-dim);
  cursor: pointer;
  border-radius: 4px;
  font-size: 0.82rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  padding: 0;
  line-height: 1;
}

.qty-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(233, 69, 96, 0.1);
}

.item-qty-value {
  min-width: 20px;
  text-align: center;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text);
}

.btn-remove-item {
  width: 22px;
  height: 22px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  border-radius: 3px;
  font-size: 0.75rem;
  transition: all 0.15s;
  flex-shrink: 0;
}

.btn-remove-item:hover {
  color: var(--red);
  background: rgba(239, 68, 68, 0.1);
}

.btn-add-item {
  display: block;
  width: 100%;
  padding: 6px;
  margin-top: 6px;
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--text-dim);
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.82rem;
  transition: all 0.15s;
}

.btn-add-item:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.btn-add-room {
  width: 100%;
  padding: 12px;
  background: transparent;
  border: 2px dashed var(--border);
  color: var(--text-dim);
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 600;
  transition: all 0.2s;
  flex-shrink: 0;
}

.btn-add-room:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* Modals */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
}

.modal {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  width: 400px;
  max-width: 90vw;
}

.modal h3 {
  margin-bottom: 16px;
  font-size: 1.1rem;
}

.modal input[type="text"],
.modal input[type="number"] {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.95rem;
  margin-bottom: 12px;
  outline: none;
  transition: border-color 0.2s;
}

.modal input:focus {
  border-color: var(--accent);
}

.dim-row {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.dim-field {
  flex: 1;
}

.dim-field label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-bottom: 4px;
}

.dim-field input {
  width: 100%;
  margin-bottom: 0 !important;
}

.preset-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  margin-bottom: 16px;
}

.preset-label {
  font-size: 0.78rem;
  color: var(--text-dim);
}

.preset-btn {
  padding: 4px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.75rem;
  transition: all 0.15s;
}

.preset-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

.btn-cancel, .btn-confirm {
  padding: 8px 20px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
}

.btn-cancel {
  background: transparent;
  color: var(--text-dim);
}

.btn-cancel:hover {
  color: var(--text);
}

.btn-confirm {
  background: var(--accent);
  color: white;
}

.btn-confirm:hover {
  background: var(--accent-dim);
}

/* Responsive */
@media (max-width: 900px) {
  .main-layout {
    flex-direction: column;
    height: auto;
    min-height: calc(100vh - 65px);
    min-height: calc(100dvh - 65px);
    overflow: visible;
  }
  .panel-left {
    width: 100%;
    min-width: 0;
    max-width: none;
    height: auto;
    max-height: 55vh;
    max-height: 55dvh;
    min-height: 200px;
    border-right: none;
    border-bottom: 1px solid var(--border);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    flex-shrink: 0;
  }
  .panel-right {
    width: 100%;
    height: 45vh;
    height: 45dvh;
    min-height: 280px;
    flex-shrink: 0;
  }
  header {
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px 16px;
  }
  header h1 { font-size: 1.1rem; }
  .header-controls {
    flex-wrap: wrap;
    gap: 8px;
  }
  .toolbar-actions { gap: 4px; }
  .toolbar-btn { padding: 6px 10px; font-size: 0.8rem; }
  .stats-bar { gap: 6px; }
  .stat { padding: 8px 6px; }
  .stat-value { font-size: 0.95rem; }
  .stat-label { font-size: 0.65rem; }
  .rooms-container {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .item-row {
    flex-wrap: wrap;
    gap: 4px;
  }
  .view-controls {
    bottom: 8px;
    right: 8px;
    gap: 4px;
  }
  .view-controls button {
    padding: 6px 10px;
    font-size: 0.75rem;
  }
}

/* Print styles */
@media print {
  body {
    background: white;
    color: #222;
  }
  header, .shared-banner, .toast, .modal-overlay, .overflow-warning {
    display: none !important;
  }
  .main-layout {
    flex-direction: column;
    height: auto;
  }
  .panel-left {
    width: 100%;
    border: none;
    overflow: visible;
    height: auto;
    max-width: none;
  }
  .panel-right {
    display: none;
  }
  .room-card {
    background: #f5f5f5;
    border: 1px solid #ddd;
    break-inside: avoid;
  }
  .room-items { display: block !important; }
  .btn-add-item, .btn-add-room, .btn-remove-item, .btn-icon, .room-chevron, .qty-btn { display: none !important; }
  .fill-bar-container { border-color: #ddd; background: #f0f0f0; }
  .fill-bar { background: #333 !important; }
  .stat { background: #f5f5f5; border-color: #ddd; }
  .stat-label, .stat-value { color: #333; }
  .item-row, .item-dims, .item-vol, .item-qty-value { color: #444; }
  .room-name { color: #222; }
  .room-volume { color: #555; }
  .view-controls { display: none; }
}
