:root {
    --primary: #2563eb;
    --primary-dark: #1e40af;
    --secondary: #64748b;
    --danger: #dc2626;
    --danger-dark: #b91c1c;
    --success: #16a34a;
    --bg-main: #f8fafc;
    --bg-sidebar: #ffffff;
    --bg-panel: #f1f5f9;
    --border: #e2e8f0;
    --text-primary: #0f172a;
    --text-secondary: #64748b;
    --shadow: 0 1px 3px rgba(0,0,0,0.1);
    --shadow-lg: 0 10px 25px rgba(0,0,0,0.1);
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg-main);
    color: var(--text-primary);
    overflow-x: hidden;
}

/* Header */
header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 60px;
    background: white;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    z-index: 100;
    box-shadow: var(--shadow);
}

header h1 {
    font-size: 20px;
    font-weight: 600;
    color: var(--primary);
}

.header-actions {
    display: flex;
    gap: 10px;
}

/* Sidebar */
.sidebar {
    position: fixed;
    top: 60px;
    left: 0;
    width: 340px;
    height: calc(100vh - 60px - 40px);
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    padding: 20px;
    z-index: 50;
}

.sidebar::-webkit-scrollbar {
    width: 8px;
}

.sidebar::-webkit-scrollbar-track {
    background: var(--bg-panel);
}

.sidebar::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
}

.sidebar::-webkit-scrollbar-thumb:hover {
    background: var(--secondary);
}

/* Sidebar Right */
.sidebar-right {
    position: fixed;
    top: 60px;
    right: 0;
    width: 340px;
    height: calc(100vh - 60px - 40px);
    background: var(--bg-sidebar);
    border-left: 1px solid var(--border);
    overflow-y: auto;
    padding: 20px;
    z-index: 50;
}

.sidebar-right::-webkit-scrollbar {
    width: 8px;
}

.sidebar-right::-webkit-scrollbar-track {
    background: var(--bg-panel);
}

.sidebar-right::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
}

.sidebar-right::-webkit-scrollbar-thumb:hover {
    background: var(--secondary);
}

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

.panel-header {
    padding: 12px 16px;
    background: white;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    user-select: none;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: background 0.2s;
}

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

.panel-header h3 {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
}

.panel-toggle {
    font-size: 12px;
    color: var(--text-secondary);
    transition: transform 0.2s;
}

.panel-toggle.collapsed {
    transform: rotate(-90deg);
}

.panel-content {
    padding: 16px;
    display: block;
}

.panel-content.collapsed {
    display: none;
}

/* Form elements */
.form-group {
    margin-bottom: 12px;
}

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

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

input[type="number"],
input[type="text"],
input[type="search"] {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    transition: all 0.2s;
    background: white;
}

input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

/* Buttons */
button {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

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

button.btn-primary:hover {
    background: var(--primary-dark);
    transform: translateY(-1px);
    box-shadow: var(--shadow);
}

button.btn-secondary {
    background: var(--secondary);
    color: white;
}

button.btn-secondary:hover {
    background: #475569;
}

button.btn-danger {
    background: var(--danger);
    color: white;
    padding: 4px 8px;
    font-size: 11px;
}

button.btn-danger:hover {
    background: var(--danger-dark);
}

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

button.btn-success:hover {
    background: #15803d;
}

button.btn-warning {
    background: #ff9800;
    color: white;
    padding: 4px 8px;
    font-size: 11px;
}

button.btn-warning:hover {
    background: #f57c00;
}

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

button.btn-block {
    width: 100%;
    justify-content: center;
}

/* Pieces list */
.search-box {
    margin-bottom: 12px;
}

.pieces-container {
    max-height: 400px;
    overflow-y: auto;
}

.piece-item {
    background: white;
    padding: 10px;
    margin-bottom: 6px;
    border-radius: 6px;
    border: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: all 0.2s;
}

.piece-item:hover {
    border-color: var(--primary);
    box-shadow: var(--shadow);
}

.piece-item-error {
    background: #ffebee;
    border-color: var(--danger);
}

.piece-item-error:hover {
    border-color: var(--danger-dark);
    box-shadow: 0 1px 3px rgba(220, 38, 38, 0.3);
}

.piece-item-editing {
    background: #e3f2fd;
    border-color: var(--primary);
    padding: 0;
}

.piece-item-editing input {
    border: 1px solid var(--border);
    border-radius: 4px;
}

.piece-item-editing input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
}

.piece-info {
    flex: 1;
}

.piece-label {
    font-weight: 600;
    font-size: 13px;
    color: var(--text-primary);
    margin-bottom: 2px;
}

.piece-dims {
    font-size: 11px;
    color: var(--text-secondary);
}

.piece-count {
    background: var(--primary);
    color: white;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 600;
    margin-right: 8px;
}

.empty-state {
    text-align: center;
    padding: 30px 20px;
    color: var(--text-secondary);
    font-size: 13px;
}

/* Main content */
.main-content {
    margin-left: 340px;
    margin-right: 340px;
    margin-top: 60px;
    margin-bottom: 40px;
    padding: 20px;
    min-height: calc(100vh - 100px);
}

/* Stats panel */
.stats-panel {
    background: white;
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 20px;
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
}

.stat-item {
    text-align: center;
}

.stat-value {
    font-size: 32px;
    font-weight: 700;
    color: var(--primary);
    margin-bottom: 4px;
}

.stat-label {
    font-size: 12px;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Controls */
.controls-bar {
    background: white;
    border-radius: 8px;
    padding: 16px;
    margin-bottom: 20px;
    border: 1px solid var(--border);
    display: flex;
    gap: 16px;
    align-items: center;
    flex-wrap: wrap;
}

.checkbox-group {
    display: flex;
    align-items: center;
    gap: 6px;
}

.checkbox-group input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
}

.checkbox-group label {
    font-size: 13px;
    color: var(--text-primary);
    cursor: pointer;
    user-select: none;
}

.panel-content .checkbox-group label {
    font-size: 12px;
}

/* Sheet results */
.sheet {
    background: white;
    border-radius: 8px;
    margin-bottom: 20px;
    border: 1px solid var(--border);
    overflow: hidden;
    box-shadow: var(--shadow);
}

.sheet-header {
    background: var(--text-primary);
    color: white;
    padding: 16px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.sheet-header h3 {
    font-size: 16px;
    font-weight: 600;
}

.sheet-body {
    padding: 20px;
}

canvas {
    display: block;
    margin: 0 auto;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: #fafafa;
}

.sheet-stats {
    margin-top: 16px;
    padding: 12px;
    background: var(--bg-panel);
    border-radius: 6px;
    font-size: 13px;
    display: flex;
    gap: 20px;
    justify-content: center;
}

.sheet-stat {
    display: flex;
    gap: 6px;
}

.sheet-stat strong {
    color: var(--text-primary);
}

.sheet-stat span {
    color: var(--text-secondary);
}

/* Footer */
footer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 40px;
    background: var(--text-primary);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    z-index: 100;
    border-top: 1px solid rgba(255,255,255,0.1);
}

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

footer a:hover {
    text-decoration: underline;
}

/* Import/Export */
.file-input {
    display: none;
}

.action-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.btn-unit {
    flex: 1;
}

/* Conversion info */
.conversion-info {
    font-size: 11px;
    color: var(--text-secondary);
    margin-top: 4px;
    font-style: italic;
}

/* Error message */
.error {
    background: #fee;
    color: var(--danger);
    padding: 12px;
    border-radius: 6px;
    border: 1px solid var(--danger);
    font-size: 13px;
    margin: 20px 0;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: white;
    border-radius: 12px;
    max-width: 600px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
}

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

.modal-header h2 {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
}

.modal-close {
    background: none;
    border: none;
    font-size: 24px;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
}

.modal-close:hover {
    background: var(--bg-panel);
    color: var(--text-primary);
}

.modal-body {
    padding: 20px;
}

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

/* Pattern preview */
.pattern-preview {
    width: 100%;
    height: 200px;
    border: 1px solid var(--border);
    border-radius: 8px;
    margin: 16px 0;
    background: #fafafa;
}

.pattern-selector {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 10px;
    margin: 16px 0;
}

.pattern-option {
    padding: 12px;
    border: 2px solid var(--border);
    border-radius: 8px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
    background: white;
}

.pattern-option:hover {
    border-color: var(--primary);
    background: var(--bg-panel);
}

.pattern-option.selected {
    border-color: var(--primary);
    background: rgba(37, 99, 235, 0.1);
}

.pattern-option-icon {
    font-size: 32px;
    margin-bottom: 8px;
}

.pattern-option-label {
    font-size: 12px;
    color: var(--text-secondary);
}

/* Zoom modal */
.zoom-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 1001;
    align-items: center;
    justify-content: center;
}

.zoom-modal.active {
    display: flex;
}

.zoom-content {
    background: white;
    border-radius: 12px;
    padding: 20px;
    max-width: 90%;
    max-height: 90vh;
    overflow: auto;
}

.zoom-canvas {
    display: block;
    margin: 0 auto;
    border: 1px solid var(--border);
    border-radius: 8px;
    max-width: 100%;
}

.piece-clickable {
    cursor: pointer;
}

/* Mobile Navigation */
.mobile-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: white;
    border-top: 1px solid var(--border);
    z-index: 200;
    height: 60px;
    box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
}

.mobile-nav-tabs {
    display: flex;
    height: 100%;
}

.mobile-nav-tab {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    border-radius: 0;
    padding: 8px;
    cursor: pointer;
    transition: all 0.2s;
    color: var(--text-secondary);
    font-size: 11px;
    gap: 4px;
}

.mobile-nav-tab:hover {
    background: var(--bg-panel);
    transform: none;
    box-shadow: none;
}

.mobile-nav-tab.active {
    color: var(--primary);
    background: var(--bg-panel);
}

.mobile-nav-icon {
    font-size: 20px;
}

.mobile-sheet-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: 150;
}

.mobile-sheet-overlay.active {
    display: block;
}

.mobile-sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: -100%;
    max-height: 80vh;
    background: white;
    border-radius: 16px 16px 0 0;
    z-index: 160;
    transition: bottom 0.3s ease-out;
    overflow: hidden;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
}

.mobile-sheet.active {
    bottom: 60px;
}

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

.mobile-sheet-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}

.mobile-sheet-close {
    background: none;
    border: none;
    font-size: 24px;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.mobile-sheet-content {
    padding: 20px;
    overflow-y: auto;
    max-height: calc(80vh - 60px);
}

.mobile-header-menu {
    display: none;
    background: none;
    border: none;
    font-size: 24px;
    color: var(--text-primary);
    cursor: pointer;
    padding: 8px;
}

.mobile-actions-menu {
    display: none;
    position: fixed;
    top: 60px;
    left: 0;
    right: 0;
    background: white;
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-lg);
    z-index: 90;
    padding: 12px;
}

.mobile-actions-menu.active {
    display: block;
}

.mobile-actions-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.mobile-actions-grid button {
    padding: 12px;
    font-size: 12px;
}

/* Language selector */
.language-selector {
    padding: 6px 12px;
    font-size: 13px;
    font-weight: 500;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg-panel);
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.2s;
    min-width: 140px;
}

.language-selector:hover {
    background: var(--bg-secondary);
    border-color: var(--primary);
}

.language-selector:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(33, 150, 243, 0.1);
}

.language-selector option {
    padding: 8px;
}

/* Responsive */
@media (max-width: 1400px) {
    .sidebar, .sidebar-right {
        width: 280px;
    }

    .main-content {
        margin-left: 280px;
        margin-right: 280px;
    }
}

@media (max-width: 1024px) {
    .sidebar, .sidebar-right {
        width: 260px;
    }

    .main-content {
        margin-left: 260px;
        margin-right: 260px;
    }

    header h1 {
        font-size: 18px;
    }

    .header-actions button {
        padding: 6px 12px;
        font-size: 12px;
    }
}

@media (max-width: 768px) {
    /* Hide desktop sidebars */
    .sidebar,
    .sidebar-right {
        display: none !important;
    }

    /* Show mobile navigation */
    .mobile-nav {
        display: block;
    }

    .mobile-header-menu {
        display: block;
    }

    /* Adjust main content */
    .main-content {
        margin-left: 0;
        margin-right: 0;
        margin-top: 60px;
        margin-bottom: 60px;
        padding: 12px;
    }

    /* Compact header */
    header {
        padding: 0 12px;
    }

    header h1 {
        font-size: 16px;
    }

    .header-actions {
        display: none;
    }

    /* Make buttons and inputs touch-friendly */
    button {
        min-height: 44px;
        padding: 10px 16px;
    }

    input[type="number"],
    input[type="text"],
    input[type="search"] {
        min-height: 44px;
        padding: 10px 14px;
        font-size: 16px;
    }

    /* Prevent zoom on input focus on iOS */
    select {
        font-size: 16px;
        min-height: 44px;
    }

    /* Adjust panels for mobile */
    .panel {
        margin-bottom: 12px;
    }

    .panel-header {
        padding: 14px;
    }

    .panel-content {
        padding: 14px;
    }

    /* Stats panel */
    .stats-panel {
        margin-bottom: 12px;
    }

    .stats-grid {
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    .stat-item {
        padding: 12px;
    }

    .stat-value {
        font-size: 20px;
    }

    .stat-label {
        font-size: 11px;
    }

    /* Piece items */
    .piece-item {
        padding: 12px;
    }

    /* Form adjustments */
    .form-group {
        margin-bottom: 14px;
    }

    .form-row {
        gap: 10px;
    }

    /* Canvas containers */
    canvas {
        max-width: 100%;
        height: auto;
    }

    .zoom-canvas {
        max-width: 100%;
    }

    /* Pattern preview */
    .pattern-preview {
        max-width: 100%;
    }

    /* Sheet results */
    .sheet-container {
        margin-bottom: 16px;
    }

    .sheet-canvas {
        max-width: 100%;
    }
}

@media (max-width: 480px) {
    header h1 {
        font-size: 14px;
    }

    .mobile-nav {
        height: 56px;
    }

    .mobile-nav-tab {
        font-size: 10px;
        padding: 6px;
    }

    .mobile-nav-icon {
        font-size: 18px;
    }

    .main-content {
        padding: 10px;
    }

    .stats-grid {
        grid-template-columns: 1fr 1fr;
    }

    .mobile-actions-grid {
        grid-template-columns: 1fr;
    }
}

@media print {
    header, .sidebar, footer, .controls-bar {
        display: none !important;
    }

    .main-content {
        margin: 0;
    }
}
