/* ================================================================
   Hangil Studio v4.0 — Premium Dobok Design Builder
   New: templates, upload, price estimator, decoration selector
   ================================================================ */

/* --- Reset & Base --- */
body {
    background-color: #0d0d0d;
    color: #e0e0e0;
    font-family: 'Pretendard', sans-serif;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

/* --- Broken Image Fallback (hide X-box) --- */
img {
    min-width: 1px;
    min-height: 1px;
}

img[src=""],
img:not([src]) {
    visibility: hidden;
}

img.img-error {
    display: none !important;
}

.draggable-item img.img-error {
    display: block !important;
    visibility: hidden;
    width: 0 !important;
    height: 0 !important;
    opacity: 0;
}

.product-item img.img-error {
    display: block !important;
    background: #2a2a2a;
    border: 1px dashed #444;
    object-fit: none;
}

.placed-item img.img-error {
    display: none !important;
}

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #444; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #666; }

/* --- Header Override --- */
header {
    background: #0d0d0d !important;
    border-bottom: 1px solid rgba(255,255,255,0.06);
    height: 56px;
    display: flex;
    align-items: center;
    padding: 0 24px;
    backdrop-filter: blur(12px);
}

header .logo { color: #fff !important; font-size: 1.1rem; text-decoration: none; }
header .logo-main { color: #fff; font-weight: 800; letter-spacing: 2px; }
header .logo-sub { color: #6c63ff; font-weight: 600; margin-left: 6px; letter-spacing: 1px; }
header nav, header .header-icons { display: none; }

/* --- Studio Grid Layout --- */
.studio-container {
    display: grid;
    grid-template-columns: 272px 1fr 300px;
    grid-template-rows: 52px 1fr;
    height: calc(100vh - 56px);
    margin-top: 56px;
}

/* --- Top Toolbar --- */
.studio-toolbar {
    grid-column: 1 / -1;
    background: #161616;
    border-bottom: 1px solid rgba(255,255,255,0.06);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    gap: 12px;
}

.view-controls {
    display: flex;
    gap: 4px;
    background: rgba(255,255,255,0.04);
    padding: 3px;
    border-radius: 8px;
}

.tool-btn {
    background: transparent;
    border: none;
    color: #888;
    padding: 7px 18px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.82rem;
    font-weight: 600;
    font-family: 'Pretendard', sans-serif;
    transition: all 0.2s;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
}

.tool-btn:hover { background: rgba(255,255,255,0.08); color: #ccc; }
.tool-btn.active { background: #6c63ff; color: white; box-shadow: 0 2px 10px rgba(108,99,255,0.3); }

.actions { display: flex; gap: 8px; align-items: center; }

.action-btn {
    background: linear-gradient(135deg, #6c63ff 0%, #5a4fff 100%);
    color: white;
    border: none;
    padding: 8px 22px;
    border-radius: 8px;
    font-weight: 700;
    font-size: 0.85rem;
    cursor: pointer;
    font-family: 'Pretendard', sans-serif;
    transition: all 0.2s;
    box-shadow: 0 2px 12px rgba(108,99,255,0.25);
}

.action-btn:hover { transform: translateY(-1px); box-shadow: 0 4px 16px rgba(108,99,255,0.35); }
.action-btn:active { transform: translateY(0); }
.action-btn:disabled { background: #333; box-shadow: none; cursor: not-allowed; }

/* --- Left Sidebar (Assets) --- */
.studio-sidebar-left {
    background: #131313;
    border-right: 1px solid rgba(255,255,255,0.06);
    padding: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.sidebar-section {
    padding: 16px;
    border-bottom: 1px solid rgba(255,255,255,0.04);
}

.sidebar-section:last-child { border-bottom: none; }

.sidebar-section h3 {
    font-size: 0.7rem;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 1.5px;
    margin: 0 0 12px 0;
    font-weight: 700;
}

/* --- Template Presets --- */
.template-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
}

.template-item {
    background: #1e1e1e;
    border: 1px solid rgba(255,255,255,0.06);
    border-radius: 10px;
    padding: 10px 4px 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    text-align: center;
}

.template-item:hover {
    border-color: rgba(108,99,255,0.4);
    background: rgba(108,99,255,0.08);
    transform: translateY(-2px);
}

.template-item.active {
    border-color: #6c63ff;
    background: rgba(108,99,255,0.12);
    box-shadow: 0 2px 12px rgba(108,99,255,0.2);
}

.template-icon {
    font-size: 1.3rem;
    line-height: 1;
}

.template-item span {
    font-size: 0.65rem;
    color: #888;
    font-weight: 600;
    white-space: nowrap;
}

.template-item.active span { color: #bbb; }

/* --- Asset Grid --- */
.asset-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.draggable-item {
    background: #1e1e1e;
    border: 1px solid rgba(255,255,255,0.06);
    border-radius: 10px;
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: grab;
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: hidden;
}

.draggable-item::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: 10px;
    background: linear-gradient(135deg, rgba(108,99,255,0.15) 0%, transparent 60%);
    opacity: 0;
    transition: opacity 0.25s;
}

.draggable-item:hover {
    border-color: rgba(108,99,255,0.4);
    transform: scale(1.05);
    box-shadow: 0 4px 16px rgba(108,99,255,0.15);
}

.draggable-item:hover::after { opacity: 1; }
.draggable-item:active { cursor: grabbing; transform: scale(0.95); }

.draggable-item img {
    max-width: 72%;
    max-height: 72%;
    pointer-events: none;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.3));
}

/* --- Text Input Tool --- */
.text-input-tool {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.text-input-tool input[type="text"],
.text-input-tool select {
    padding: 10px 14px;
    background: #1a1a1a;
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 8px;
    color: #eee;
    font-size: 0.88rem;
    font-family: 'Pretendard', sans-serif;
    transition: border-color 0.2s;
}

.text-input-tool input[type="text"]:focus,
.text-input-tool select:focus {
    outline: none;
    border-color: #6c63ff;
    box-shadow: 0 0 0 3px rgba(108,99,255,0.12);
}

.text-options-row {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    align-items: center;
}

.color-picker-wrap {
    position: relative;
    width: 38px;
    height: 38px;
}

.color-picker-wrap input[type="color"] {
    width: 100%;
    height: 100%;
    border: 2px solid rgba(255,255,255,0.1);
    border-radius: 8px;
    cursor: pointer;
    background: none;
    padding: 2px;
}

.color-picker-wrap input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
.color-picker-wrap input[type="color"]::-webkit-color-swatch { border: none; border-radius: 5px; }

.text-add-btn {
    width: 100%;
    padding: 10px;
    background: linear-gradient(135deg, #6c63ff 0%, #5a4fff 100%);
    border: none;
    border-radius: 8px;
    color: white;
    font-weight: 700;
    font-size: 0.85rem;
    cursor: pointer;
    font-family: 'Pretendard', sans-serif;
    transition: all 0.2s;
}

.text-add-btn:hover { box-shadow: 0 2px 12px rgba(108,99,255,0.3); }

/* --- Upload Tool --- */
.upload-tool {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.upload-btn {
    width: 100%;
    padding: 12px;
    background: #1e1e1e;
    border: 2px dashed rgba(108,99,255,0.3);
    border-radius: 10px;
    color: #aaa;
    font-weight: 600;
    font-size: 0.82rem;
    cursor: pointer;
    font-family: 'Pretendard', sans-serif;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.upload-btn:hover {
    border-color: #6c63ff;
    background: rgba(108,99,255,0.06);
    color: #ccc;
}

.upload-btn svg {
    flex-shrink: 0;
}

.upload-hint {
    font-size: 0.68rem;
    color: #555;
    text-align: center;
    margin: 0;
}

.upload-preview {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(108,99,255,0.06);
    border: 1px solid rgba(108,99,255,0.15);
    border-radius: 8px;
    padding: 8px;
    margin-top: 4px;
}

.upload-preview img {
    width: 36px;
    height: 36px;
    object-fit: contain;
    border-radius: 6px;
    background: #fff;
}

.upload-filename {
    font-size: 0.72rem;
    color: #aaa;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
}

/* --- Center Canvas Area --- */
.studio-canvas-area {
    background: #0d0d0d;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
    background-image: radial-gradient(circle, rgba(255,255,255,0.03) 1px, transparent 1px);
    background-size: 24px 24px;
}

.canvas-wrapper {
    position: relative;
    filter: drop-shadow(0 8px 40px rgba(0,0,0,0.4));
}

.canvas-hint {
    color: #444;
    font-size: 0.82rem;
    margin-top: 16px;
    letter-spacing: 0.5px;
}

/* --- Process Info Bar --- */
.process-info-bar {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    justify-content: center;
    margin-top: 14px;
    padding: 0 12px;
}

.info-chip {
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(255,255,255,0.05);
    border: 1px solid rgba(255,255,255,0.08);
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 0.72rem;
    color: #888;
    white-space: nowrap;
}

.info-chip strong {
    color: #ccc;
    font-weight: 700;
}

.info-icon { font-size: 0.82rem; }

.dobok-canvas {
    width: 480px;
    height: 540px;
    background: transparent;
    position: relative;
    display: none;
    border-radius: 4px;
}

.dobok-canvas.active { display: block; }

.dobok-base {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    pointer-events: none;
}

/* --- Placed Items (Patches/Text) --- */
.placed-item {
    position: absolute;
    cursor: move;
    z-index: 10;
    user-select: none;
    touch-action: none;
    transition: filter 0.15s;
}

.placed-item:hover { filter: brightness(1.1); }

.placed-item.selected {
    outline: 2px solid #6c63ff;
    outline-offset: 4px;
    filter: brightness(1.05);
}

.placed-item .remove-btn {
    position: absolute;
    top: -12px;
    right: -12px;
    width: 22px;
    height: 22px;
    background: #ff4757;
    color: white;
    font-size: 11px;
    font-weight: bold;
    border-radius: 50%;
    display: none;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 20;
    box-shadow: 0 2px 8px rgba(255,71,87,0.4);
    border: 2px solid #0d0d0d;
}

.placed-item.selected .remove-btn { display: flex; }

.placed-item .resize-handle {
    position: absolute;
    bottom: -7px;
    right: -7px;
    width: 14px;
    height: 14px;
    background: #6c63ff;
    border: 2px solid #0d0d0d;
    border-radius: 3px;
    cursor: se-resize;
    display: none;
    z-index: 20;
    box-shadow: 0 2px 6px rgba(108,99,255,0.3);
}

.placed-item.selected .resize-handle { display: block; }

.placed-text {
    font-family: 'Pretendard', sans-serif;
    font-weight: 800;
    white-space: nowrap;
    pointer-events: none;
    text-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

/* --- Right Sidebar (Product Select + Price) --- */
.studio-sidebar-right {
    background: #131313;
    border-left: 1px solid rgba(255,255,255,0.06);
    padding: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

/* --- Price Estimator --- */
.price-estimator {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.est-input-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.est-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.est-field label {
    font-size: 0.68rem;
    color: #666;
    font-weight: 600;
}

.est-field input,
.est-field select {
    padding: 8px 10px;
    background: #1a1a1a;
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 8px;
    color: #eee;
    font-size: 0.82rem;
    font-family: 'Pretendard', sans-serif;
    transition: border-color 0.2s;
}

.est-field input:focus,
.est-field select:focus {
    outline: none;
    border-color: #6c63ff;
}

.price-estimate-result {
    background: rgba(108,99,255,0.04);
    border: 1px solid rgba(108,99,255,0.1);
    border-radius: 10px;
    padding: 12px;
}

.est-breakdown {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.est-row {
    display: flex;
    justify-content: space-between;
    font-size: 0.78rem;
    color: #888;
}

.est-row.est-unit {
    border-top: 1px solid rgba(255,255,255,0.06);
    padding-top: 6px;
    margin-top: 2px;
    color: #bbb;
    font-weight: 600;
}

.est-row.est-total {
    border-top: 1px solid rgba(108,99,255,0.2);
    padding-top: 8px;
    margin-top: 4px;
    color: #6c63ff;
    font-weight: 800;
    font-size: 0.88rem;
}

.est-note {
    display: block;
    font-size: 0.68rem;
    color: #555;
    margin-top: 6px;
    line-height: 1.4;
}

/* --- Product Search & List --- */
.sidebar-section .product-search {
    width: 100%;
    padding: 9px 12px 9px 34px;
    background: #1a1a1a;
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 8px;
    color: #ddd;
    font-size: 0.85rem;
    font-family: 'Pretendard', sans-serif;
    margin-bottom: 12px;
    transition: border-color 0.2s;
    box-sizing: border-box;
}

.sidebar-section .product-search:focus {
    outline: none;
    border-color: #6c63ff;
}

.product-search-wrap {
    position: relative;
    margin-bottom: 12px;
}

.product-search-wrap::before {
    content: '🔍';
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 0.8rem;
    z-index: 1;
}

.product-search-wrap .product-search {
    margin-bottom: 0;
}

.product-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.product-item {
    background: #1a1a1a;
    border: 1px solid rgba(255,255,255,0.06);
    border-radius: 10px;
    padding: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 12px;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.product-item:hover {
    background: #1e1e1e;
    border-color: rgba(255,255,255,0.12);
    transform: translateX(-2px);
}

.product-item.active {
    border-color: #6c63ff;
    background: rgba(108,99,255,0.08);
    box-shadow: inset 0 0 0 1px rgba(108,99,255,0.2);
}

.product-item img {
    width: 52px;
    height: 52px;
    object-fit: contain;
    background: #fff;
    border-radius: 8px;
    flex-shrink: 0;
}

.product-item .p-info { flex: 1; min-width: 0; }

.product-item .p-name {
    font-size: 0.85rem;
    color: #eee;
    margin: 0;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.product-item .p-cat {
    font-size: 0.72rem;
    color: #666;
    margin: 3px 0 0;
}

.product-item .p-price {
    font-size: 0.72rem;
    color: #6c63ff;
    font-weight: 700;
    margin: 2px 0 0;
}

.product-count {
    font-size: 0.72rem;
    color: #555;
    text-align: center;
    padding: 8px 0 4px;
}

/* --- Modal Overlay --- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.75);
    backdrop-filter: blur(8px);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.modal-content {
    background: #fff;
    color: #333;
    border-radius: 20px;
    max-width: 540px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    padding: 2rem;
    position: relative;
    box-shadow: 0 24px 80px rgba(0,0,0,0.4);
}

.modal-close {
    position: absolute;
    top: 16px;
    right: 16px;
    background: #f5f5f5;
    border: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    font-size: 1.4rem;
    color: #666;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.modal-close:hover { background: #eee; color: #333; }
.modal-content h2 { font-size: 1.4rem; font-weight: 800; margin-bottom: 0.3rem; }
.modal-desc { color: #777; font-size: 0.9rem; margin-bottom: 1.5rem; line-height: 1.5; }

.modal-preview {
    background: #f8f9fa;
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 1.5rem;
    text-align: center;
    border: 1px solid #eee;
    min-height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-preview img { max-width: 100%; max-height: 300px; border-radius: 8px; }

.modal-form { display: flex; flex-direction: column; gap: 1rem; }
.modal-form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.modal-form-group { display: flex; flex-direction: column; }
.modal-form-group label { font-size: 0.85rem; font-weight: 600; margin-bottom: 6px; color: #333; }

.modal-form-group input,
.modal-form-group textarea {
    padding: 10px 14px;
    border: 1px solid #ddd;
    border-radius: 10px;
    font-size: 0.95rem;
    font-family: 'Pretendard', sans-serif;
    transition: all 0.2s;
}

.modal-form-group input:focus,
.modal-form-group textarea:focus {
    outline: none;
    border-color: #6c63ff;
    box-shadow: 0 0 0 3px rgba(108,99,255,0.1);
}

.modal-form-group textarea { resize: vertical; }

/* --- Mobile Panels --- */
.mobile-panel-toggle {
    display: none;
    position: fixed;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 100;
    gap: 8px;
}

.mobile-panel-btn {
    background: rgba(22,22,22,0.95);
    backdrop-filter: blur(12px);
    border: 1px solid rgba(255,255,255,0.1);
    color: #ccc;
    padding: 10px 20px;
    border-radius: 12px;
    font-size: 0.82rem;
    font-weight: 600;
    cursor: pointer;
    font-family: 'Pretendard', sans-serif;
}

.mobile-panel-btn.active { background: #6c63ff; color: white; border-color: #6c63ff; }

/* --- Responsive --- */
@media (max-width: 1200px) {
    .studio-container { grid-template-columns: 240px 1fr 260px; }
}

@media (max-width: 1024px) {
    .studio-container {
        grid-template-columns: 1fr;
        grid-template-rows: 48px 1fr;
    }

    .studio-sidebar-left,
    .studio-sidebar-right {
        position: fixed;
        top: 104px;
        bottom: 0;
        width: 300px;
        z-index: 200;
        transform: translateX(-100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: 4px 0 24px rgba(0,0,0,0.3);
    }

    .studio-sidebar-left { left: 0; }
    .studio-sidebar-right { right: 0; left: auto; transform: translateX(100%); }

    .studio-sidebar-left.open { transform: translateX(0); }
    .studio-sidebar-right.open { transform: translateX(0); }

    .mobile-panel-toggle { display: flex; }

    .mobile-overlay {
        position: fixed;
        inset: 0;
        background: rgba(0,0,0,0.5);
        z-index: 150;
        display: none;
    }

    .mobile-overlay.show { display: block; }

    .dobok-canvas {
        width: 85vw;
        height: 85vw;
        max-width: 480px;
        max-height: 540px;
    }

    .studio-toolbar {
        padding: 0 12px;
        gap: 6px;
    }

    .tool-btn { padding: 6px 12px; font-size: 0.78rem; }
    .action-btn { padding: 7px 14px; font-size: 0.8rem; }

    .modal-form-row { grid-template-columns: 1fr; }

    .template-grid { grid-template-columns: repeat(4, 1fr); }
}

@media (max-width: 640px) {
    header { padding: 0 12px; height: 50px; }

    .studio-container { grid-template-rows: 44px 1fr; height: calc(100vh - 50px); }

    .studio-toolbar { overflow-x: auto; }

    .dobok-canvas {
        width: 90vw;
        height: 90vw;
        max-width: 400px;
        max-height: 450px;
    }

    .studio-sidebar-left,
    .studio-sidebar-right { width: 85vw; max-width: 320px; top: 94px; }

    .canvas-hint { font-size: 0.75rem; }

    .template-grid { grid-template-columns: repeat(2, 1fr); }

    .est-input-row { grid-template-columns: 1fr; }

    .process-info-bar { gap: 4px; }
    .info-chip { padding: 4px 10px; font-size: 0.68rem; }
}
