:root {
    --bg-color: #111111;
    --panel-bg: rgba(30, 30, 30, 0.7);
    --panel-border: rgba(255, 255, 255, 0.1);
    --text-main: #f0f0f0;
    --text-dim: #a0a0a0;
    --accent: #5a8dee;
    --accent-hover: #7aa2f7;
    --danger: #ff4b4b;
    --danger-hover: #ff7676;
    --drill-color: #ff7730;
    --intact-color: #00dd77;
    --axis-front: #5a8dee;
    --axis-side: #ef4444;
    --axis-top: #22c55e;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

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

#canvas-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    touch-action: none;
}

#ui-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: center;
    padding: 24px;
    padding-bottom: calc(32px + env(safe-area-inset-bottom, 0px));
}

/* While a custom dropdown is open, lift #ui-container above #proj-panel
   (z-index 1500) so the option list isn't painted over by it. Without
   this, #ui-container's own z-index (10) traps the dropdown's z-index
   inside its stacking context, no matter how high the dropdown's own
   z-index is set. */
#ui-container.select-open {
    z-index: 2001;
}

.panel {
    background: var(--panel-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--panel-border);
    border-radius: 12px;
    padding: 16px;
    pointer-events: auto;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
    user-select: none;
    font-size: 13px;
    width: 95%;
    max-width: 390px;
}

.main-title {
    position: absolute;
    top: 24px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 100;
    font-size: 28px;
    font-weight: 600;
    margin: 0;
    background: linear-gradient(135deg, var(--drill-color), #ffaa55);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    pointer-events: none;
    text-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    white-space: nowrap;
}

.cut-instructions {
    position: absolute;
    top: 72px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 100;
    background: rgba(50, 50, 50, 0.8);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid var(--panel-border);
    padding: 8px 16px;
    border-radius: 20px;
    color: #fff;
    font-size: 14px;
    pointer-events: none;
    box-shadow: 0 4px 20px rgba(0,0,0,0.4);
    transition: all 0.3s ease;
    white-space: nowrap;
}

.cut-instructions.warning {
    color: #ff4b4b;
    border: 1px solid rgba(255, 75, 75, 0.3);
    background: rgba(255, 75, 75, 0.1);
}

.startup-guide {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 2000;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid rgba(255, 119, 48, 0.5);
    padding: 24px 48px 24px 32px;
    border-radius: 16px;
    color: #fff;
    font-size: 15px;
    font-weight: 500;
    box-shadow: 0 12px 48px rgba(0,0,0,0.5);
    transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    white-space: nowrap;
}

.startup-guide.hidden {
    opacity: 0;
    transform: translate(-50%, calc(-50% - 20px));
    pointer-events: none;
}

.close-guide-btn {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.6);
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    padding: 4px;
    transition: color 0.2s;
    min-height: auto;
    width: auto;
}

.close-guide-btn:hover {
    color: #fff;
    background: none;
}

/* ---- HUD ---- */
.hud-stats {
    position: absolute;
    top: 12px;
    left: 12px;
    z-index: 1000;
    background: rgba(50, 50, 50, 0.8);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    padding: 8px 12px;
    border-radius: 12px;
    color: #fff;
    min-width: 130px;
    pointer-events: none;
    transition: opacity 0.3s ease, transform 0.3s ease;
    box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}

.hud-stats.hidden {
    opacity: 0;
    transform: translateY(-10px);
}

.hud-row {
    display: flex;
    justify-content: flex-start;
    align-items: baseline;
    gap: 4px;
    margin-bottom: 2px;
}

.hud-row:last-child {
    margin-bottom: 0;
}

.hud-label {
    font-size: 11px;
    font-weight: 500;
    color: rgba(255, 255, 255, 0.5);
    white-space: nowrap;
}

.hud-value {
    font-size: 14px;
    font-weight: 800;
    font-family: 'Outfit', 'Inter', sans-serif;
}

.hud-value.holed {
    color: var(--drill-color);
    text-shadow: 0 0 10px rgba(255, 119, 48, 0.4);
}

.hud-value.intact {
    color: var(--intact-color);
    text-shadow: 0 0 10px rgba(0, 221, 119, 0.3);
}

/* ---- Preset Section ---- */
.preset-section {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.preset-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-main);
    opacity: 0.9;
}

.preset-controls {
    display: flex;
    gap: 8px;
    width: 100%;
}

.preset-section label {
    font-size: 11px;
    color: var(--text-dim);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

/* ---- Custom Select (replaces native <select> so the option list can be
   forced to open upward — the panel sits at the bottom of the screen, and
   on many mobile browsers a native <select> opening downward from there
   gets clipped by the viewport, leaving some options unreachable) ---- */
.custom-select {
    position: relative;
    flex: 1;
}

.custom-select.wide { flex: 2; }

.custom-select-trigger {
    width: 100%;
    padding: 10px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--panel-border);
    border-radius: 6px;
    color: var(--text-main);
    font-family: inherit;
    font-size: 14px;
    font-weight: 400;
    cursor: pointer;
    outline: none;
    transition: all 0.2s ease;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    text-align: left;
    white-space: nowrap;
}

.custom-select-trigger-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.custom-select-trigger-label.placeholder {
    color: var(--text-dim);
}

.custom-select-arrow {
    flex-shrink: 0;
    font-size: 11px;
    color: var(--text-dim);
    transition: transform 0.2s ease;
}

.custom-select.open .custom-select-arrow {
    transform: rotate(180deg);
}

.custom-select-trigger:hover {
    background: rgba(255, 255, 255, 0.1);
}

.custom-select.open .custom-select-trigger {
    background: rgba(255, 255, 255, 0.1);
    border-color: var(--accent);
}

/* Options list opens UPWARD (bottom: 100%) on purpose — this panel is
   docked to the bottom of the viewport, so opening downward is the one
   direction guaranteed to run out of room on a phone. */
.custom-select-options {
    position: absolute;
    left: 0;
    right: 0;
    bottom: calc(100% + 6px);
    background: #2a2a2a;
    border: 1px solid var(--panel-border);
    border-radius: 8px;
    padding: 4px;
    max-height: 45vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    box-shadow: 0 -12px 32px rgba(0, 0, 0, 0.5);
    z-index: 3000;
    display: none;
}

.custom-select.open .custom-select-options {
    display: block;
}

.custom-select-option {
    padding: 10px 12px;
    font-size: 14px;
    color: #fff;
    border-radius: 5px;
    cursor: pointer;
    white-space: nowrap;
}

.custom-select-option:hover,
.custom-select-option.highlighted {
    background: rgba(255, 255, 255, 0.12);
}

.custom-select-option.selected {
    color: var(--accent);
    font-weight: 600;
}

/* ---- Generic button styles ---- */
button {
    flex: 1;
    padding: 12px 8px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--panel-border);
    border-radius: 8px;
    color: var(--text-main);
    font-family: inherit;
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
    min-height: 44px;
}

button:hover {
    background: rgba(255, 255, 255, 0.15);
}

button.active {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

.hidden {
    display: none !important;
}

.actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.action-btn {
    font-size: 14px !important;
    padding: 10px 8px !important;
    min-height: 44px;
}

/* ---- Options Accordion ---- */
.options-header {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--panel-border);
    border-radius: 8px 8px 0 0;
    padding: 8px 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-dim);
    transition: background 0.2s ease, color 0.2s ease;
    margin-top: 10px;
    pointer-events: auto;
}

.options-header:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-main);
}

.options-header.expanded {
    position: relative;
    height: 0;
    margin: 0;
    padding: 0;
    border-top: 1px solid var(--panel-border);
    border-left: 1px solid var(--panel-border);
    border-right: 1px solid var(--panel-border);
    border-radius: 8px 8px 0 0;
    z-index: 100;
}

.options-header.expanded #drill-options-icon {
    position: absolute;
    right: 8px;
    top: 10px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 4px;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.options-header.expanded span:first-child {
    display: none;
}

.options-content {
    display: flex;
    flex-direction: column;
    gap: 4px;
    width: 100%;
    padding: 8px;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--panel-border);
    border-top: none;
    border-radius: 0 0 8px 8px;
    overflow: hidden;
    transition: max-height 0.3s ease, padding 0.3s ease, opacity 0.3s ease, border-color 0.3s ease;
    max-height: 400px;
}

.options-content.collapsed {
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    opacity: 0;
    border-color: rgba(255, 255, 255, 0);
}

#drill-options-icon {
    font-size: 14px;
    font-family: monospace;
    width: 20px;
    text-align: center;
}

/* ---- Checkbox Toggle Switch ---- */
.checkbox-group {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 0.75rem;
    padding: 2px 0;
}

.checkbox-group label:not(.switch) {
    font-size: 12px;
    color: var(--text-main);
    cursor: pointer;
    user-select: none;
}

.switch {
    position: relative;
    display: inline-block;
    width: 34px;
    height: 18px;
    flex-shrink: 0;
}

.switch input { opacity: 0; width: 0; height: 0; }

.slider {
    position: absolute;
    cursor: pointer;
    top: 0; left: 0; right: 0; bottom: 0;
    background-color: rgba(255, 255, 255, 0.1);
    transition: .3s;
    border-radius: 18px;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.slider:before {
    position: absolute;
    content: "";
    height: 12px; width: 12px;
    left: 2px; bottom: 2px;
    background-color: #94a3b8;
    transition: .3s;
    border-radius: 50%;
}

input:checked + .slider { background-color: var(--accent); border-color: var(--accent); }
input:checked + .slider:before { transform: translateX(16px); background-color: #fff; }

/* ---- Floating Side Buttons ---- */
.side-button {
    position: fixed;
    right: 24px;
    z-index: 1000;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    color: white;
    text-decoration: none;
    transition: all 0.3s ease;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    cursor: pointer;
    box-sizing: border-box;
    padding: 0;
}

.home-button { top: 24px; }
.zoom-button { top: 128px; }
.trash-button, .clear-button { top: 76px; }

#btn-toggle-presets { top: 252px; }
#btn-mode-build { top: 324px; }
#btn-mode-delete { top: 376px; }
#btn-mode-drill { top: 428px; }
#btn-toggle-projection { top: 180px; }

.side-button:hover {
    background: var(--accent);
    border-color: var(--accent);
    transform: scale(1.1);
    box-shadow: 0 0 15px rgba(79, 172, 254, 0.4);
}

.side-button.active {
    background: var(--accent);
    border-color: var(--accent);
    box-shadow: 0 0 20px rgba(79, 172, 254, 0.6);
}

/* Drill mode button active: use drill color */
#btn-mode-drill.active {
    background: rgba(255, 85, 68, 0.85);
    border-color: #ff5544;
    box-shadow: 0 0 20px rgba(255, 85, 68, 0.5);
}

.side-button svg {
    width: 20px;
    height: 20px;
    stroke: currentColor;
}

/* ---- History Controls (Undo/Redo) ---- */
#history-dashboard {
    position: fixed;
    top: 116px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 12px;
    z-index: 2000;
    pointer-events: none;
    transition: top 0.4s cubic-bezier(0.18, 0.89, 0.32, 1.28);
}

#history-dashboard.moved-up {
    top: 72px;
}

.history-btn {
    pointer-events: auto;
    background: rgba(30, 30, 40, 0.6);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: var(--text-main);
    width: 36px;
    height: 36px;
    min-width: 36px;
    min-height: 36px;
    aspect-ratio: 1 / 1;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    padding: 0;
}

.history-btn svg {
    width: 16px;
    height: 16px;
    margin: 0;
}

.history-btn:hover:not(:disabled) {
    background: rgba(50, 50, 70, 0.8);
    transform: translateY(-2px) scale(1.05);
    border-color: rgba(255, 255, 255, 0.2);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
}

.history-btn:active:not(:disabled) {
    transform: translateY(0) scale(0.95);
}

.history-btn:disabled {
    opacity: 0.15;
    cursor: not-allowed;
    filter: grayscale(1);
    box-shadow: none;
}

/* ---- Projection Panel (3方向同時表示) ---- */
.proj-panel {
    position: fixed;
    top: 96px;
    left: 16px;
    width: 166px;
    z-index: 1500;
    background: var(--panel-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--panel-border);
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
    overflow: hidden;
    pointer-events: auto;
    user-select: none;
}

.proj-panel.hidden {
    display: none !important;
}

.proj-panel-header {
    padding: 7px 12px;
    background: rgba(255, 255, 255, 0.05);
    border-bottom: 1px solid var(--panel-border);
}

.proj-panel-title {
    font-size: 12px;
    font-weight: 700;
    color: var(--text-main);
    letter-spacing: 0.02em;
}

.proj-views-list {
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.proj-view-item {
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.proj-view-label {
    font-size: 10px;
    font-weight: 700;
    color: var(--text-dim);
    letter-spacing: 0.05em;
}

.proj-canvas {
    width: 100%;
    aspect-ratio: 1 / 1;
    display: block;
    border-radius: 5px;
    border: 2px solid rgba(255, 255, 255, 0.08);
    background: #0c0c0c;
    cursor: default;
    transition: border-color 0.2s, box-shadow 0.2s;
}

/* Axis colors — match the drill-mode axis names (front/side/top) */
.proj-view-label.axis-front { color: var(--axis-front); }
.proj-view-label.axis-side  { color: var(--axis-side); }
.proj-view-label.axis-top   { color: var(--axis-top); }
.proj-canvas.axis-front { border-color: rgba(90, 141, 238, 0.4); }
.proj-canvas.axis-side  { border-color: rgba(239, 68, 68, 0.4); }
.proj-canvas.axis-top   { border-color: rgba(34, 197, 94, 0.4); }

/* When drill mode is active, the projection cells become clickable */
.proj-panel.drill-mode .proj-canvas {
    cursor: pointer;
}
.proj-panel.drill-mode .proj-canvas:hover {
    filter: brightness(1.25);
    box-shadow: 0 0 10px rgba(255, 119, 48, 0.35);
}

/* ---- Mobile ---- */
@media (max-width: 600px) {
    .panel { max-width: 330px !important; }

    .main-title { font-size: 20px; top: 16px; }

    .hud-stats { top: 8px; left: 8px; padding: 6px 10px; min-width: auto; }
    .hud-label { font-size: 9px; }
    .hud-value { font-size: 13px; }
    .hud-row { gap: 3px; margin-bottom: 1px; }

    .cut-instructions { font-size: 11px; padding: 6px 12px; top: 56px; }
    .custom-select-trigger { font-size: 13px; padding: 8px 10px; }
    .custom-select-option { font-size: 13px; padding: 9px 10px; }
    .action-btn { min-height: 32px !important; padding: 4px 8px !important; font-size: 11px !important; }
    .pc-only { display: none; }
    .checkbox-group label:not(.switch) { font-size: 10px !important; }

    .side-button {
        width: 38px; height: 38px;
        min-width: 38px; min-height: 38px;
        aspect-ratio: 1 / 1;
        flex-shrink: 0;
        right: 16px;
    }
    .side-button svg { width: 18px; height: 18px; }

    #history-dashboard { top: 100px; gap: 10px; }
    .history-btn { width: 32px; height: 32px; }

    .proj-panel {
        top: auto;
        left: 0;
        right: 64px;
        bottom: calc(146px + env(safe-area-inset-bottom, 0px));
        width: auto;
        border-radius: 12px 12px 0 0;
        border-left: none;
    }
    .proj-panel-header { display: none; }
    .proj-views-list { flex-direction: row; padding: 8px 10px; gap: 10px; }
    .proj-view-item { flex: 1; }
    .proj-panel-title { font-size: 11px; }
    .proj-view-label { font-size: 9px; text-align: center; }

    /* Mobile display order: 真横→正面→真上 (DOM order / data-axis stays
       unchanged for click handling; this only reorders visually) */
    .proj-view-item[data-axis="x"] { order: 1; } /* 真横 */
    .proj-view-item[data-axis="z"] { order: 2; } /* 正面 */
    .proj-view-item[data-axis="y"] { order: 3; } /* 真上 */
}
