/* App Title Styled */
.app-title {
  position: absolute;
  top: 30px;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  font-family: 'Outfit', sans-serif;
  font-size: 2.2rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  background: linear-gradient(135deg, #fff 0%, #cbd5e1 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 10px 20px rgba(0,0,0,0.3);
  z-index: 100;
  white-space: nowrap;
  pointer-events: none;
}

@media (max-width: 650px) {
  .app-title {
    font-size: 1.5rem;
    top: 20px;
  }
}

:root {
  --bg-main: #0f172a;
  --bg-side: rgba(30, 41, 59, 0.9);
  --accent: #fbbf24;
  --accent-blue: #38bdf8;
  --text: #f1f5f9;
  --text-dim: #cbd5e1; /* Brightened for better contrast */
  --text-secondary: #94a3b8;
  --glass: rgba(15, 23, 42, 0.7);
  --border: rgba(255, 255, 255, 0.1);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
.hidden { display: none !important; }

.app-container { 
  position: relative;
  width: 100vw;
  height: 100dvh;
  overflow: hidden;
  user-select: none; /* Prevent text selection on long press */
  -webkit-user-select: none;
  -webkit-touch-callout: none; /* Prevent context menu on iOS */
  touch-action: pinch-zoom; /* Allow zoom but block single-finger scroll */
}

.main-content {
  position: absolute;
  top: 0; left: 0; width: 100%; height: 100%;
  background: radial-gradient(circle at center, #1e293b 0%, #0f172a 100%);
}

canvas { 
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%; 
  z-index: 1; /* Lowest layer */
  touch-action: pinch-zoom; /* Allow zooming but block panning to prevent drag interference */
}

.header-controls {
  position: absolute;
  top: calc(4.5rem + env(safe-area-inset-top)) !important;
  left: 50% !important;
  transform: translateX(-50%) !important;
  display: flex !important;
  gap: 1.5rem !important;
  z-index: 1000;
  width: max-content;
  pointer-events: none; /* Let clicks through to canvas if between panels */
}
@media (max-width: 500px) {
  .header-controls {
    flex-direction: column !important; /* Stack vertically at the top */
    gap: 0.5rem !important;
    top: 4.5rem !important; /* Clear the title */
    left: 50% !important;
    transform: translateX(-50%) !important;
    width: min(90%, 300px) !important;
    align-items: center !important;
  }
  .floating-panel {
    padding: 0.5rem !important;
  }
  /* Further reduce width to fit side-by-side on small screens */
  .mode-panel, .config-panel {
    width: 175px !important;
    flex-shrink: 1 !important;
  }
}
.header-controls > * { pointer-events: auto; }

.floating-panel {
  position: absolute;
  background: var(--glass);
  backdrop-filter: blur(16px);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 1.25rem;
  box-shadow: 0 15px 40px rgba(0,0,0,0.5);
  pointer-events: auto;
  touch-action: pinch-zoom; /* Block browser scroll when interacting with panels */
  z-index: 100; /* Higher than canvas */
  color: var(--text); /* Enforce light color for all children */
}

/* 1. Palette Panel (Bottom Center) */
.palette-panel {
  top: auto !important;
  bottom: calc(2rem + env(safe-area-inset-bottom)) !important;
  left: 50% !important;
  transform: translateX(-50%) !important;
  right: auto !important;
  display: flex !important;
  flex-direction: column !important;
  align-items: center !important;
  gap: 8px !important;
}

/* Help Bubble (Speech Bubble) */
.help-bubble {
  position: absolute;
  top: 0;
  left: 0;
  transform: translate(-50%, -100%) translateY(-20px);
  opacity: 0;
  background: rgba(15, 23, 42, 0.95);
  backdrop-filter: blur(10px);
  color: #fff;
  padding: 12px 20px;
  border-radius: 14px;
  font-size: 0.85rem;
  min-width: 230px;
  font-weight: 500;
  line-height: 1.4;
  text-align: center;
  box-shadow: 0 10px 25px rgba(0,0,0,0.4), 0 0 15px rgba(56, 189, 248, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.1);
  z-index: 110;
  pointer-events: none;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.help-bubble.visible {
  opacity: 1;
  transform: translate(-50%, -100%) translateY(-55px);
}
.help-bubble::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border-width: 8px;
  border-style: solid;
  border-color: rgba(15, 23, 42, 0.95) transparent transparent transparent;
}
.help-bubble.hidden { display: none; }

.palette-options { display: flex; gap: 15px; align-items: center; }

.pallet-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 12px;
  transition: all 0.2s;
  touch-action: pinch-zoom; /* Allow zooming but block dragging-to-scroll */
}
.pallet-item:hover { background: rgba(255, 255, 255, 0.05); }
.pallet-item.active { background: rgba(56, 189, 248, 0.15); }
.pallet-item span { font-size: 0.75rem; font-weight: 600; color: var(--text-dim); }
.pallet-item.active span { color: var(--accent-blue); }

.pallet-icon { border-radius: 4px; position: relative; box-shadow: 0 4px 10px rgba(0,0,0,0.3); }
.weight-10 { background: linear-gradient(135deg, #b45309 0%, #fbbf24 50%, #b45309 100%); width: 26px; height: 32px; }
.weight-1 { background: linear-gradient(135deg, #94a3b8 0%, #f1f5f9 50%, #94a3b8 100%); width: 18px; height: 22px; margin-top: 10px; }
.weight-clear { 
    width: 32px; height: 32px; 
    background: rgba(56, 189, 248, 0.1); 
    border: 1px solid rgba(56, 189, 248, 0.3); 
    border-radius: 50%; 
    display: flex; align-items: center; justify-content: center; 
    font-size: 1.2rem; 
    box-shadow: 0 0 10px rgba(56, 189, 248, 0.2);
}
.pallet-item#paletteClear:hover .weight-clear {
    background: var(--accent-blue);
    color: #fff;
    box-shadow: 0 0 15px var(--accent-blue);
    transform: scale(1.1) rotate(15deg);
}
.weight-trash { 
    width: 32px; height: 32px; 
    background: rgba(239, 68, 68, 0.1); 
    border: 1px solid rgba(239, 68, 68, 0.3); 
    border-radius: 50%; 
    display: flex; align-items: center; justify-content: center; 
    font-size: 1.2rem; 
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Deletion highlight */
.pallet-item.is-deletion-active {
    background: rgba(239, 68, 68, 0.3) !important;
    transform: translateY(-5px) scale(1.1);
    box-shadow: 0 10px 20px rgba(239, 68, 68, 0.4);
}
.pallet-item.is-deletion-active .weight-trash {
    background: #ef4444;
    border-color: #fff;
    color: #fff;
    box-shadow: 0 0 15px #ef4444;
}

/* 3. Bar Config Panel */
.config-panel {
  position: relative !important;
  width: 280px;
}
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  margin-bottom: 0.75rem;
}

.panel-header h3 {
  font-size: 0.85rem;
  color: var(--accent-blue);
  margin: 0;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.panel-content {
  transition: all 0.3s ease;
  overflow: hidden;
  max-height: 500px;
  opacity: 1;
}

.collapsible.collapsed .panel-content {
  max-height: 0;
  opacity: 0;
  margin: 0;
}

.collapsible.collapsed .panel-header {
  margin-bottom: 0;
}

.toggle-btn {
  position: absolute;
  top: 6px;
  right: 15px;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: var(--text-dim);
  width: 20px;
  height: 20px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  z-index: 10;
}

.toggle-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
}

.toggle-btn::before {
  content: '-';
  font-family: Arial, sans-serif;
  font-weight: bold;
  font-size: 1rem;
}

.collapsible.collapsed .toggle-btn::before {
  content: '+';
}

/* 4. Mode Select Panel */
.mode-panel {
  position: relative !important;
  width: 280px;
}
.mode-panel label { color: var(--text); }

/* Inputs */
.checkbox-row { display: flex; align-items: center; gap: 0.6rem; cursor: pointer; font-size: 0.85rem; color: var(--text); white-space: nowrap; }
.control-group label { white-space: nowrap; display: block; }
select, input[type="range"] { width: 100%; outline: none; }
select { background: #1e293b; color: #fff; border: 1px solid var(--border); padding: 0.5rem; border-radius: 8px; font-size: 0.85rem; }
option { background: #1e293b; color: #fff; }
input[type="range"] { -webkit-appearance: none; height: 4px; background: var(--border); border-radius: 2px; margin: 0.5rem 0; }
input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 16px; height: 16px; background: var(--accent-blue); border-radius: 50%; cursor: pointer; }


.balance-badge {
  position: fixed;
  bottom: calc(1.5rem + 115px); /* Just above the palette panel */
  left: 50%;
  transform: translateX(-50%) translateY(0);
  background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
  color: #fff;
  padding: 0.6rem 2rem;
  border-radius: 999px;
  font-family: 'Outfit', sans-serif;
  font-weight: 700;
  font-size: 1.25rem;
  box-shadow: 0 10px 25px rgba(245, 158, 11, 0.4), 0 0 20px rgba(251, 191, 36, 0.3);
  z-index: 1000;
  pointer-events: none;
  transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  opacity: 1;
  border: 2px solid rgba(255, 255, 255, 0.3);
  text-shadow: 0 2px 4px rgba(0,0,0,0.1);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  white-space: nowrap;
}

.balance-badge.hidden, .floating-panel.hidden {
  opacity: 0;
  transform: translateX(-50%) translateY(20px) scale(0.8);
  pointer-events: none;
}

/* Responsive Adjustments for Mobile */
@media (max-width: 650px) {
  .balance-badge {
    bottom: calc(1.5rem + 105px);
    font-size: 1.1rem;
    padding: 0.5rem 1.5rem;
  }
  .palette-panel {
    top: auto !important;
    bottom: 1.5rem !important;
    left: 50% !important;
    transform: translateX(-50%) !important;
    width: auto;
    padding: 0.75rem 1.5rem;
  }
  .palette-options { gap: 2rem; }
  
  .mode-panel {
    position: relative !important;
    top: auto !important;
    left: auto !important;
    width: 100% !important;
    max-width: 240px;
    padding: 0.5rem 0.75rem !important;
  }
  
  .config-panel {
    position: relative !important;
    top: auto !important;
    right: auto !important;
    left: auto !important;
    width: 100% !important;
    max-width: 240px;
    padding: 0.5rem 0.75rem !important;
  }

  .guide-panel {
    display: none; /* Hide guide on mobile to save space */
  }

  .formula-panel {
    bottom: 7rem !important; /* Move above the palette */
    padding: 0.75rem 1.5rem;
    font-size: 0.6rem;
  }
  
  .floating-panel h3 { font-size: 0.75rem; margin-bottom: 0.4rem; }
  .checkbox-row, label, select { font-size: 0.75rem; }
}

body {
    font-family: 'Inter', 'Noto Sans JP', sans-serif;
    margin: 0;
    padding: 0;
    background-color: var(--bg-color);
    color: var(--text-main);
    overflow: hidden;
    width: 100vw;
    height: 100vh;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

/* Home Button Styles */
.portal-link-btn-mini {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    background: var(--glass);
    backdrop-filter: blur(12px);
    border: 1px solid var(--border);
    border-radius: 50%;
    color: var(--text);
    text-decoration: none;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    pointer-events: auto;
}

.portal-link-btn-mini:hover {
    background: var(--accent-blue);
    border-color: var(--accent-blue);
    transform: translateY(-2px) scale(1.05);
    box-shadow: 0 6px 15px rgba(56, 189, 248, 0.4);
    color: #fff;
}

@media (max-width: 650px) {
    #top-nav {
        top: 10px !important;
        right: 10px !important;
    }
}

.help-bubble.center-align.visible {
  transform: translate(-50%, -50%);
}
