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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    padding: 20px;
}

.container {
    width: 90%;
    margin: 0 5%;
    background: white;
    border-radius: 10px;
    padding: 40px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}

h1 {
    color: #333;
    text-align: center;
    margin-bottom: 10px;
}

.subtitle {
    text-align: center;
    color: #666;
    margin-bottom: 30px;
}

.upload-section {
    background: #f8f9fa;
    padding: 30px;
    border-radius: 8px;
    margin-bottom: 30px;
}

.form-group {
    margin-bottom: 20px;
}

label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    color: #333;
}

input[type="file"] {
    width: 100%;
    padding: 10px;
    border: 2px dashed #667eea;
    border-radius: 5px;
    background: white;
}

select, input[type="number"] {
    width: 100%;
    padding: 10px;
    border: 2px solid #ddd;
    border-radius: 5px;
    background: white;
    font-size: 14px;
}

.hint {
    display: block;
    margin-top: 5px;
    font-size: 12px;
    color: #666;
    font-style: italic;
}

.dimension-group {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.dimension-group input[type="number"] {
    flex: 0 0 100px;
    width: 100px;
}

.dimension-label {
    font-size: 14px;
    color: #666;
    white-space: nowrap;
}

input[type="range"] {
    width: 100%;
    height: 6px;
    border-radius: 3px;
    background: #ddd;
    outline: none;
    -webkit-appearance: none;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #667eea;
    cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #667eea;
    cursor: pointer;
    border: none;
}

.range-labels {
    display: flex;
    justify-content: space-between;
    margin-top: 5px;
    font-size: 12px;
    color: #666;
}

label span {
    color: #667eea;
    font-weight: bold;
}

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

.checkbox-group label {
    display: flex;
    align-items: center;
    cursor: pointer;
    font-weight: normal;
}

.checkbox-group input[type="checkbox"] {
    width: auto;
    margin-right: 10px;
    cursor: pointer;
}

button {
    background: #667eea;
    color: white;
    border: none;
    padding: 12px 30px;
    border-radius: 5px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.3s;
}

button:hover {
    background: #5568d3;
}

button:disabled {
    background: #ccc;
    cursor: not-allowed;
}

.loading {
    text-align: center;
    padding: 40px;
}

.spinner {
    border: 4px solid #f3f3f3;
    border-top: 4px solid #667eea;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    animation: spin 1s linear infinite;
    margin: 0 auto 20px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.hidden {
    display: none !important;
}

.results {
    margin-top: 30px;
}

h2, h3 {
    color: #333;
    margin: 20px 0 15px;
}

#patternInfo {
    background: #f8f9fa;
    padding: 15px;
    border-radius: 5px;
    margin-bottom: 20px;
}

.info-item {
    margin: 5px 0;
    color: #555;
}

#palette {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: 15px;
    margin-bottom: 30px;
}

.color-item {
    display: flex;
    align-items: stretch;
    border-radius: 8px;
    border: 2px solid #333;
    overflow: hidden;
    /* Background color is set dynamically to the actual pattern color */
}

.color-item-left {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 10px;
    gap: 8px;
    min-width: 80px;
}

.color-checkbox {
    width: 20px;
    height: 20px;
    cursor: pointer;
    flex-shrink: 0;
}

.color-swatch {
    width: 50px;
    height: 50px;
    border-radius: 5px;
    border: 2px solid rgba(0, 0, 0, 0.3);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.color-symbol {
    font-size: 24px;
    font-weight: bold;
    text-align: center;
    text-shadow: 0 0 3px rgba(255, 255, 255, 0.8), 0 0 6px rgba(255, 255, 255, 0.8);
}

.color-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: rgba(255, 255, 255, 0.95);
    padding: 12px;
    min-width: 0;
}

.color-info-row {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.color-info-label {
    font-size: 10px;
    font-weight: 600;
    color: #667eea;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.color-name-input {
    border: 1px solid #ddd;
    border-radius: 3px;
    padding: 6px 8px;
    font-size: 13px;
    font-weight: 600;
    background: white;
    width: 100%;
    box-sizing: border-box;
}

.color-name-input:focus {
    outline: 2px solid #667eea;
    border-color: #667eea;
}

.dmc-inputs {
    display: flex;
    gap: 6px;
}

.dmc-code-input,
.dmc-name-input {
    border: 1px solid #ddd;
    border-radius: 3px;
    padding: 5px 6px;
    font-size: 12px;
    background: white;
    box-sizing: border-box;
}

.dmc-code-input {
    width: 65px;
    flex-shrink: 0;
}

.dmc-name-input {
    flex: 1;
    min-width: 0;
}

.dmc-code-input:focus,
.dmc-name-input:focus {
    outline: 2px solid #667eea;
    border-color: #667eea;
}

.color-hex {
    font-size: 11px;
    color: #666;
    font-family: monospace;
    font-weight: 600;
}

.color-count {
    font-size: 12px;
    color: #999;
    font-weight: 600;
}

.color-picker-row {
    display: flex;
    gap: 6px;
    align-items: center;
}

.color-picker-input {
    width: 60px;
    height: 32px;
    border: 1px solid #ddd;
    border-radius: 3px;
    cursor: pointer;
    background: white;
}

.color-picker-input:focus {
    outline: 2px solid #667eea;
    border-color: #667eea;
}

.reset-color-btn {
    flex: 1;
    padding: 6px 12px;
    background: #dc3545;
    color: white;
    border: none;
    border-radius: 3px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 600;
    transition: background 0.2s;
}

.reset-color-btn:hover {
    background: #c82333;
}

.thread-calculator {
    background: #f8f9fa;
    padding: 20px;
    border-radius: 8px;
    margin-bottom: 20px;
    max-height: 2000px;
    overflow: hidden;
    transition: max-height 0.3s ease-out, opacity 0.3s ease-out;
    opacity: 1;
}

.thread-calculator.collapsed {
    max-height: 0;
    opacity: 0;
    padding: 0;
    margin-bottom: 0;
    transition: max-height 0.3s ease-in, opacity 0.2s ease-in;
}

.calculator-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 15px;
    flex-wrap: wrap;
}

.calculator-controls label {
    margin: 0;
    font-weight: 600;
}

.calculator-controls input[type="number"] {
    width: 80px;
}

#threadAmounts {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 12px;
}

.thread-amount-item {
    background: white;
    padding: 12px;
    border-radius: 5px;
    border: 2px solid #ddd;
    display: flex;
    align-items: center;
    gap: 12px;
}

.thread-amount-color {
    width: 30px;
    height: 30px;
    border-radius: 4px;
    border: 1px solid #333;
    flex-shrink: 0;
}

.thread-amount-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.thread-amount-name {
    font-weight: 600;
    font-size: 13px;
    color: #333;
}

.thread-amount-details {
    font-size: 12px;
    color: #666;
}

.thread-amount-length {
    font-size: 13px;
    font-weight: 600;
    color: #667eea;
}

.pattern-controls {
    margin: 15px 0;
    display: flex;
    gap: 10px;
}

.pattern-controls button {
    padding: 8px 20px;
    font-size: 14px;
}

#patternGrid {
    border: 2px solid #333;
    display: inline-block;
    background: white;
    max-width: 100%;
    overflow-x: auto;
}

.pattern-row {
    display: flex;
    line-height: 1;
}

.pattern-cell {
    width: 20px;
    height: 20px;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: bold;
    position: relative;
    cursor: pointer;
    box-sizing: border-box;
}

.pattern-cell.has-edge::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    border: 2px solid #000;
    pointer-events: none;
}

.pattern-cell.done {
    opacity: 0.4;
    position: relative;
}

.pattern-cell.done::before {
    content: '✓';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 16px;
    color: #00ff00;
    text-shadow: 0 0 3px #000;
    font-weight: bold;
    z-index: 10;
    pointer-events: none;
}

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

.modal-content {
    background: white;
    padding: 30px;
    border-radius: 10px;
    max-width: 500px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
}

.modal-content h2 {
    margin-top: 0;
}

.modal-content input[type="text"] {
    width: 100%;
    padding: 10px;
    border: 2px solid #ddd;
    border-radius: 5px;
    font-size: 14px;
    margin: 15px 0;
}

.modal-buttons {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    margin-top: 20px;
}

#patternList {
    max-height: 400px;
    overflow-y: auto;
}

.pattern-item {
    padding: 15px;
    border: 2px solid #e9ecef;
    border-radius: 5px;
    margin-bottom: 10px;
    transition: all 0.2s;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

.pattern-item:hover {
    border-color: #667eea;
    background: #f8f9fa;
}

.pattern-item-content {
    flex: 1;
    cursor: pointer;
}

.pattern-item-name {
    font-weight: 600;
    color: #333;
    margin-bottom: 5px;
}

.pattern-item-details {
    font-size: 12px;
    color: #666;
}

.delete-btn {
    padding: 8px 16px;
    background: #dc3545;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 14px;
    transition: background 0.2s;
    flex-shrink: 0;
}

.delete-btn:hover {
    background: #c82333;
}

.cell-context-menu {
    position: fixed;
    background: white;
    border: 2px solid #667eea;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    padding: 0;
    z-index: 2000;
    min-width: 180px;
}

.cell-context-menu-header {
    padding: 10px 15px;
    background: #667eea;
    color: white;
    font-weight: 600;
    font-size: 14px;
    border-radius: 6px 6px 0 0;
    text-align: center;
}

.cell-context-menu-item {
    padding: 12px 15px;
    cursor: pointer;
    font-size: 14px;
    transition: background 0.2s;
    border-bottom: 1px solid #eee;
}

.cell-context-menu-item:last-child {
    border-bottom: none;
    border-radius: 0 0 6px 6px;
}

.cell-context-menu-item:hover {
    background: #f0f0f0;
}

.cell-context-menu-item.undo {
    color: #d9534f;
    font-weight: 600;
}

.cell-context-menu-item.cancel {
    color: #666;
}

.cell-context-menu-item.set-index {
    color: #667eea;
    font-weight: 600;
}

.cell-context-menu-dmc {
    padding: 10px 15px;
    background: #fff8e1;
    border-bottom: 1px solid #ffa726;
    font-size: 13px;
    color: #333;
}

.cell-context-menu-index {
    padding: 10px 15px;
    background: #f0f8ff;
    border-bottom: 1px solid #667eea;
    font-size: 13px;
    color: #333;
    font-weight: 600;
}

.pattern-cell.index-point {
    box-shadow: 0 0 0 3px #ff6b6b inset, 0 0 8px 2px #ff6b6b;
    z-index: 5;
    position: relative;
}

.pattern-cell.index-point::after {
    content: '⊕';
    position: absolute;
    top: 2px;
    right: 2px;
    font-size: 14px;
    color: #ff6b6b;
    font-weight: bold;
    text-shadow: 0 0 2px white, 0 0 4px white;
    pointer-events: none;
}

/* Collapsible upload section */
.toggle-section-btn {
    width: 100%;
    padding: 12px;
    background: #667eea;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 15px;
    transition: background 0.2s;
    text-align: left;
}

.toggle-section-btn:hover {
    background: #5568d3;
}

.upload-form-content {
    max-height: 2000px;
    overflow: hidden;
    transition: max-height 0.3s ease-out, opacity 0.3s ease-out;
    opacity: 1;
}

.upload-form-content.collapsed {
    max-height: 0;
    opacity: 0;
    transition: max-height 0.3s ease-in, opacity 0.2s ease-in;
}

.zoom-hint {
    font-size: 13px;
    color: #666;
    text-align: center;
    margin: 10px 0;
    font-style: italic;
}

/* Collapsible palette section */
.toggle-palette-btn {
    padding: 6px 12px;
    background: #667eea;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 600;
    margin-left: 10px;
    transition: background 0.2s;
}

.toggle-palette-btn:hover {
    background: #5568d3;
}

.palette-content {
    max-height: 2000px;
    overflow: hidden;
    transition: max-height 0.3s ease-out, opacity 0.3s ease-out;
    opacity: 1;
}

.palette-content.collapsed {
    max-height: 0;
    opacity: 0;
    transition: max-height 0.3s ease-in, opacity 0.2s ease-in;
}

/* Authentication Styles */
.auth-header {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    margin-bottom: 20px;
    padding: 10px;
    background: #f8f9fa;
    border-radius: 8px;
}

.auth-user-info {
    display: flex;
    align-items: center;
    gap: 15px;
}

.auth-user-info span {
    font-weight: 500;
    color: #333;
}

.auth-error {
    color: #dc3545;
    margin: 10px 0;
    padding: 10px;
    background: #f8d7da;
    border: 1px solid #f5c6cb;
    border-radius: 4px;
    display: none;
}

.auth-error:not(:empty) {
    display: block;
}

.btn-link {
    background: none;
    border: none;
    color: #667eea;
    cursor: pointer;
    padding: 5px;
    text-decoration: underline;
    font-size: 14px;
}

.btn-link:hover {
    color: #5568d3;
}

#authModal .form-group {
    margin-bottom: 15px;
}

#authModal .form-group small {
    display: block;
    margin-top: 5px;
    color: #666;
    font-size: 12px;
}

#authModal .form-group input {
    width: 100%;
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 14px;
}

#authModal .form-group input:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

#authModal .modal-buttons {
    display: flex;
    gap: 10px;
    margin-top: 20px;
    flex-wrap: wrap;
}

#authModal .modal-buttons button {
    flex: 1;
    min-width: 100px;
}

/* Loading overlay for authentication */
.auth-loading {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
}

.auth-loading-spinner {
    background: white;
    padding: 30px;
    border-radius: 8px;
    text-align: center;
}

.auth-loading-spinner::after {
    content: '';
    display: block;
    width: 40px;
    height: 40px;
    margin: 10px auto 0;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #667eea;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}
