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

:root {
  --bg: #ffffff;
  --bg-secondary: #f7f7f8;
  --bg-tertiary: #efefef;
  --text: #1a1a1a;
  --text-secondary: #555555;
  --text-muted: #888888;
  --border: #e0e0e0;
  --border-strong: #cccccc;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-light: #dbeafe;
  --success: #16a34a;
  --success-light: #dcfce7;
  --warning: #d97706;
  --warning-light: #fef3c7;
  --danger: #dc2626;
  --danger-light: #fee2e2;
  --radius: 6px;
  --radius-lg: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.12);
  --transition: 150ms ease;
  --header-height: 52px;
  --sidebar-width: 280px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', monospace;
}

html, body {
  height: 100%;
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* --- Header --- */
.app-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-height);
  background: var(--text);
  color: var(--bg);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  z-index: 100;
  border-bottom: 1px solid #333;
}

.app-header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.app-title {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.app-version {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 400;
}

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

/* --- Phase Navigation --- */
.phase-nav {
  display: flex;
  gap: 2px;
  background: rgba(255,255,255,0.1);
  border-radius: var(--radius);
  padding: 2px;
}

.phase-btn {
  padding: 6px 16px;
  border: none;
  background: transparent;
  color: rgba(255,255,255,0.6);
  cursor: pointer;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 500;
  transition: all var(--transition);
  font-family: var(--font);
}

.phase-btn:hover {
  color: rgba(255,255,255,0.9);
  background: rgba(255,255,255,0.08);
}

.phase-btn.active {
  color: var(--text);
  background: var(--bg);
}

/* --- Language selector --- */
.lang-select {
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.2);
  color: var(--bg);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  font-family: var(--font);
}

.lang-select option {
  color: var(--text);
  background: var(--bg);
}

/* --- Main Layout --- */
.app-body {
  margin-top: var(--header-height);
  min-height: calc(100vh - var(--header-height) - 48px);
  display: flex;
}

.phase-content {
  display: none;
  width: 100%;
}

.phase-content.active {
  display: flex;
}

/* --- Sidebar --- */
.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  border-right: 1px solid var(--border);
  background: var(--bg-secondary);
  overflow-y: auto;
  height: calc(100vh - var(--header-height) - 48px);
  position: sticky;
  top: var(--header-height);
}

.sidebar-section {
  border-bottom: 1px solid var(--border);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: #ffffff;
  background: var(--text);
  cursor: default;
}

.sidebar-header.collapsible {
  cursor: pointer;
}

.sidebar-header.collapsible:hover {
  background: #333;
}

.sidebar-header.sub {
  background: var(--bg-tertiary);
  color: var(--text-secondary);
}

.sidebar-section-body {
  overflow: hidden;
}

.sidebar-section-body.collapsed {
  display: none;
}

.sidebar-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  cursor: pointer;
  transition: background var(--transition);
  font-size: 13px;
  border-left: 3px solid transparent;
}

.sidebar-item:hover {
  background: var(--bg-tertiary);
}

.sidebar-item.active {
  background: var(--primary-light);
  border-left-color: var(--primary);
  font-weight: 500;
}

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

.sidebar-item-actions {
  display: flex;
  gap: 4px;
  opacity: 0.3;
}

.sidebar-item:hover .sidebar-item-actions {
  opacity: 1;
}

.sidebar-indent {
  padding-left: 32px;
}

.sidebar-empty {
  padding: 16px;
  color: var(--text-muted);
  font-size: 12px;
  text-align: center;
}

/* --- Main Area --- */
.main-area {
  flex: 1;
  overflow: auto;
  padding: 24px;
  height: calc(100vh - var(--header-height) - 48px);
}

.main-area-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 12px;
}

.main-area-title {
  font-size: 20px;
  font-weight: 600;
}

.main-area-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* --- Grid Editor --- */

.grid-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-wrap: wrap;
}

.grid-toolbar-group {
  display: flex;
  align-items: center;
  gap: 4px;
}

.grid-toolbar-separator {
  width: 1px;
  height: 24px;
  background: var(--border);
  margin: 0 4px;
}

.grid-info {
  font-size: 12px;
  color: var(--text-muted);
  margin-left: auto;
}

.grid {
  display: inline-grid;
  gap: 1px;
  background: var(--border);
  padding: 1px;
  cursor: crosshair;
}

.grid-cell {
  aspect-ratio: 1;
  background: var(--bg);
  position: relative;
  user-select: none;
  -webkit-user-select: none;
}

.grid-cell:hover {
  background: #eef2ff;
}

.grid-cell.selected {
  background: #93b4f5 !important;
  outline: 2px solid var(--primary);
  outline-offset: -2px;
}

.grid-cell.selected:hover {
  background: #7da4f0 !important;
}

.grid-cell.room-cell {
  cursor: pointer;
}

.grid-cell .room-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-weight: 600;
  color: white;
  text-shadow: 0 1px 2px rgba(0,0,0,0.3);
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}

.grid-cell.room-cell.highlight-group {
  outline: 3px solid var(--warning);
  outline-offset: -3px;
  z-index: 2;
}

.grid-cell.room-cell.room-free {
  box-shadow: inset 0 0 0 2px var(--success);
}

.grid-cell.room-cell.room-occupied {
  box-shadow: inset 0 0 0 2px var(--danger);
  opacity: 0.85;
}

/* Room tooltip */
.room-tooltip {
  position: fixed;
  background: var(--text);
  color: var(--bg);
  padding: 8px 12px;
  border-radius: var(--radius);
  font-size: 12px;
  max-width: 280px;
  z-index: 200;
  pointer-events: none;
  box-shadow: var(--shadow-lg);
  line-height: 1.6;
}

.room-tooltip-name {
  font-weight: 600;
  margin-bottom: 4px;
  font-size: 13px;
}

.room-tooltip-field {
  display: flex;
  gap: 8px;
}

.room-tooltip-label {
  color: var(--text-muted);
}

/* --- Grid layout with edge buttons --- */
.grid-layout {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.grid-edge-row {
  display: flex;
  align-items: stretch;
  gap: 4px;
}

.grid-edge {
  display: flex;
  gap: 2px;
}

.grid-edge-h {
  justify-content: center;
}

.grid-edge-v {
  flex-direction: column;
  justify-content: center;
}

.grid-edge-btn {
  width: 24px;
  height: 24px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all var(--transition);
  padding: 0;
  line-height: 1;
  font-family: var(--font);
}

.grid-edge-btn:hover {
  background: var(--primary-light);
  border-color: var(--primary);
  color: var(--primary);
}

.grid-inner {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-secondary);
  overflow: auto;
  max-width: calc(100vw - var(--sidebar-width) - 100px);
}

.grid-cell.room-active {
  outline: 2px solid var(--text);
  outline-offset: -2px;
  z-index: 3;
}

.grid.grid-merge-mode {
  cursor: cell;
}

.grid.grid-merge-mode .grid-cell:not(.room-cell):hover {
  background: var(--primary-light);
}

/* --- Room List View --- */
.room-list-controls {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
  align-items: center;
}

.room-list-table {
  width: 100%;
  border-collapse: collapse;
}

.room-list-table th,
.room-list-table td {
  padding: 8px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.room-list-table th {
  font-weight: 600;
  color: var(--text-secondary);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  background: var(--bg-secondary);
  position: sticky;
  top: 0;
}

.room-list-table tr:hover td {
  background: var(--bg-secondary);
}

.room-color-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  display: inline-block;
  vertical-align: middle;
  margin-right: 8px;
}

.status-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 500;
}

.status-badge.free {
  background: var(--success-light);
  color: var(--success);
}

.status-badge.assigned {
  background: var(--danger-light);
  color: var(--danger);
}

.group-badge {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 11px;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  margin: 1px 2px;
}

.warning-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
  background: var(--warning-light);
  color: var(--warning);
}

/* --- Buttons --- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  font-family: var(--font);
  white-space: nowrap;
}

.btn:hover {
  background: var(--bg-secondary);
}

.btn-primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.btn-primary:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

.btn-danger {
  background: var(--danger);
  color: white;
  border-color: var(--danger);
}

.btn-danger:hover {
  background: #b91c1c;
}

.btn-success {
  background: var(--success);
  color: white;
  border-color: var(--success);
}

.btn-sm {
  padding: 4px 8px;
  font-size: 12px;
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 4px;
  font-size: 16px;
  transition: all var(--transition);
}

.btn-icon:hover {
  background: var(--bg-tertiary);
  color: var(--text);
}

.btn-icon.active {
  background: var(--primary-light);
  color: var(--primary);
}

.btn-group {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.btn-group .btn {
  border: none;
  border-radius: 0;
  border-right: 1px solid var(--border);
}

.btn-group .btn:last-child {
  border-right: none;
}

.btn-group .btn.active {
  background: var(--primary);
  color: white;
}

/* --- Forms --- */
.form-group {
  margin-bottom: 16px;
}

.form-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.form-input,
.form-select,
.form-textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  transition: border-color var(--transition);
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

.form-textarea {
  min-height: 80px;
  resize: vertical;
}

.form-inline {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

.form-inline .form-group {
  flex: 1;
  margin-bottom: 0;
}

/* --- Select/Filter controls --- */
.filter-bar {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.filter-select {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
}

.search-input {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  font-family: var(--font);
  min-width: 200px;
}

.search-input:focus {
  outline: none;
  border-color: var(--primary);
}

/* --- Modal --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 150ms ease;
}

.modal {
  background: var(--bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  min-width: 380px;
  max-width: 560px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  animation: slideUp 200ms ease;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-title {
  font-size: 16px;
  font-weight: 600;
}

.modal-close {
  font-size: 20px;
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
}

/* --- Toast --- */
.toast {
  position: fixed;
  bottom: 60px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  padding: 10px 20px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  z-index: 2000;
  opacity: 0;
  transition: all 300ms ease;
  box-shadow: var(--shadow-lg);
}

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

.toast-success {
  background: var(--success);
  color: white;
}

.toast-error {
  background: var(--danger);
  color: white;
}

.toast-warning {
  background: var(--warning);
  color: white;
}

/* --- Footer --- */
.app-footer {
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 12px;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
  padding: 0 20px;
  flex-wrap: wrap;
}

.app-footer a {
  color: var(--text-secondary);
  text-decoration: none;
  font-weight: 500;
}

.app-footer a:hover {
  color: var(--primary);
  text-decoration: underline;
}

/* --- Tabs --- */
.tabs {
  display: flex;
  border-bottom: 2px solid var(--border);
  margin-bottom: 20px;
  gap: 0;
}

.tab {
  padding: 8px 16px;
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: all var(--transition);
  font-family: var(--font);
}

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

.tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

/* --- Empty State --- */
.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-muted);
}

.empty-state-icon {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.3;
}

.empty-state-text {
  font-size: 14px;
  margin-bottom: 16px;
}

/* --- Panel --- */
.panel {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--bg);
  overflow: hidden;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  font-weight: 600;
}

.panel-body {
  padding: 16px;
}

/* --- Custom fields list --- */
.field-list-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}

.field-list-item:last-child {
  border-bottom: none;
}

.field-list-name {
  flex: 1;
  font-weight: 500;
  font-size: 13px;
}

.field-list-type {
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg-tertiary);
  padding: 2px 6px;
  border-radius: 4px;
}

/* --- Changelog --- */
.changelog-container {
  max-width: 700px;
  margin: 0 auto;
  padding: 32px 24px;
}

.changelog-entry {
  margin-bottom: 32px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--border);
}

.changelog-entry:last-child {
  border-bottom: none;
}

.changelog-version {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 4px;
}

.changelog-date {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.changelog-list {
  list-style: none;
  padding: 0;
}

.changelog-list li {
  padding: 4px 0 4px 16px;
  position: relative;
  font-size: 13px;
  color: var(--text-secondary);
}

.changelog-list li::before {
  content: '—';
  position: absolute;
  left: 0;
  color: var(--text-muted);
}

/* --- Animations --- */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* --- View toggle area --- */
.view-area {
  display: none;
}
.view-area.active {
  display: block;
}

/* --- Mobile warning --- */
.mobile-warning {
  display: none;
  background: var(--warning-light);
  color: var(--warning);
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  margin-bottom: 16px;
}

/* --- Room edit panel (inline) --- */
.room-edit-panel {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  background: var(--bg-secondary);
  margin-bottom: 16px;
}

.room-edit-panel-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* --- Responsive --- */
@media (max-width: 768px) {
  .sidebar {
    display: none;
  }

  .main-area {
    padding: 16px;
  }

  .mobile-warning {
    display: block;
  }

  .grid-layout {
    display: none;
  }

  .app-header {
    padding: 0 12px;
    flex-wrap: wrap;
    height: auto;
    min-height: var(--header-height);
    gap: 8px;
    padding-top: 8px;
    padding-bottom: 8px;
  }

  .phase-nav {
    order: 3;
    width: 100%;
  }

  .phase-btn {
    flex: 1;
    text-align: center;
    padding: 6px 8px;
    font-size: 12px;
  }

  .modal {
    min-width: auto;
    margin: 16px;
    max-width: calc(100vw - 32px);
  }

  .room-list-controls {
    flex-direction: column;
    align-items: stretch;
  }

  .search-input {
    min-width: auto;
  }

  .filter-bar {
    flex-direction: column;
    align-items: stretch;
  }
}

/* --- Export/Import bar --- */
.io-bar {
  display: flex;
  gap: 8px;
  padding: 8px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  align-items: center;
}

.file-input-hidden {
  display: none;
}
