/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --color-red: #e74c3c;
    --color-blue: #3498db;
    --color-yellow: #f39c12;
    --color-black: #2c3e50;
    --color-wild: #9b59b6;
    --color-bg: #f8f9fa;
    --color-surface: #ffffff;
    --color-text: #2c3e50;
    --color-text-light: #7f8c8d;
    --color-border: #dfe6e9;
    --color-primary: #27ae60;
    --color-secondary: #95a5a6;
    --color-danger: #e74c3c;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 4px 16px rgba(0, 0, 0, 0.15);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: var(--color-bg);
    color: var(--color-text);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.container {
    max-width: 800px;
    margin: 0 auto;
    padding: 16px;
}

/* Header */
header {
    text-align: center;
    padding: 24px 0;
    margin-bottom: 24px;
}

header h1 {
    font-size: 2rem;
    margin-bottom: 8px;
}

.subtitle {
    color: var(--color-text-light);
    font-size: 0.95rem;
}

header {
    position: relative;
}

/* Hamburger Menu */
.menu-container {
    position: absolute;
    top: 0;
    right: 0;
}

.menu-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 12px;
    touch-action: manipulation;
    display: flex;
    align-items: center;
    justify-content: center;
}

.hamburger-icon {
    display: block;
    width: 24px;
    height: 3px;
    background: var(--color-text);
    border-radius: 2px;
    position: relative;
    transition: background 0.2s;
}

.hamburger-icon::before,
.hamburger-icon::after {
    content: '';
    position: absolute;
    left: 0;
    width: 24px;
    height: 3px;
    background: var(--color-text);
    border-radius: 2px;
    transition: transform 0.2s;
}

.hamburger-icon::before {
    top: -8px;
}

.hamburger-icon::after {
    top: 8px;
}

.menu-btn:hover .hamburger-icon,
.menu-btn:hover .hamburger-icon::before,
.menu-btn:hover .hamburger-icon::after {
    background: var(--color-primary);
}

.menu-dropdown {
    position: absolute;
    top: 100%;
    right: 0;
    background: var(--color-surface);
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    min-width: 150px;
    z-index: 100;
    animation: fadeIn 0.2s ease;
}

.menu-item {
    display: block;
    width: 100%;
    padding: 12px 16px;
    background: none;
    border: none;
    text-align: left;
    cursor: pointer;
    font-size: 1rem;
    color: var(--color-text);
    transition: background 0.2s;
}

.menu-item:hover {
    background: var(--color-bg);
}

.menu-item:active {
    background: var(--color-border);
}

/* Sections */
.section {
    background: var(--color-surface);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: var(--shadow);
}

.section h2 {
    font-size: 1.25rem;
    margin-bottom: 16px;
    color: var(--color-text);
}

.section h3 {
    font-size: 1rem;
    margin-bottom: 12px;
    color: var(--color-text);
}

/* Tile Picker */
.tile-picker {
    margin-bottom: 24px;
}

.color-group {
    margin-bottom: 16px;
}

.color-label {
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.color-group.red .color-label { color: var(--color-red); }
.color-group.blue .color-label { color: var(--color-blue); }
.color-group.yellow .color-label { color: var(--color-yellow); }
.color-group.black .color-label { color: var(--color-black); }
.color-group.wild .color-label { color: var(--color-wild); }

.tiles-row {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(44px, 1fr));
    gap: 8px;
}

.tile-btn {
    min-width: 44px;
    min-height: 44px;
    border: 2px solid currentColor;
    border-radius: 8px;
    background: var(--color-surface);
    color: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    touch-action: manipulation;
    position: relative;
}

.tile-btn:active {
    transform: scale(0.95);
}

.tile-btn.red { color: var(--color-red); }
.tile-btn.blue { color: var(--color-blue); }
.tile-btn.yellow { color: var(--color-yellow); }
.tile-btn.black { color: var(--color-black); }
.tile-btn.wild-btn { color: var(--color-wild); }

.tile-btn:hover {
    background: currentColor;
    color: white;
}

.tile-btn.selected {
    background: currentColor;
    color: white;
}

.tile-btn .count {
    position: absolute;
    top: -8px;
    right: -8px;
    background: var(--color-danger);
    color: white;
    font-size: 0.7rem;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
}

/* Current Hand Display */
.current-hand {
    margin-top: 20px;
}

.hand-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.hand-display {
    min-height: 80px;
    padding: 16px;
    background: var(--color-bg);
    border-radius: 8px;
    margin-bottom: 12px;
}

.hand-tiles {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.hand-tile {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    border-radius: 6px;
    font-weight: 600;
    font-size: 0.9rem;
    border: 2px solid currentColor;
}

.hand-tile.red { color: var(--color-red); background: #fee; }
.hand-tile.blue { color: var(--color-blue); background: #eef; }
.hand-tile.yellow { color: var(--color-yellow); background: #ffd; }
.hand-tile.black { color: var(--color-black); background: #eee; }
.hand-tile.wild { color: var(--color-wild); background: #f5f; }

.hand-tile .remove-btn {
    background: none;
    border: none;
    color: inherit;
    cursor: pointer;
    font-size: 1.1rem;
    padding: 0;
    margin-left: 4px;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.hand-tile .remove-btn:hover {
    opacity: 1;
}

.empty-message {
    color: var(--color-text-light);
    font-style: italic;
    text-align: center;
}

/* Table Display */
.table-display {
    min-height: 80px;
    padding: 16px;
    background: var(--color-bg);
    border-radius: 8px;
    margin-bottom: 16px;
}

.table-meld {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px;
    background: var(--color-surface);
    border-radius: 8px;
    margin-bottom: 8px;
    border: 1px solid var(--color-border);
}

.meld-type-badge {
    font-size: 0.75rem;
    padding: 4px 8px;
    border-radius: 4px;
    background: var(--color-secondary);
    color: white;
    text-transform: uppercase;
    font-weight: 600;
}

.meld-number {
    font-weight: 700;
    color: var(--color-text-secondary);
    min-width: 24px;
}

.meld-tiles {
    display: flex;
    gap: 6px;
    flex: 1;
    flex-wrap: wrap;
}

.meld-tile {
    padding: 4px 8px;
    border-radius: 4px;
    font-weight: 600;
    font-size: 0.85rem;
}

.meld-tile.red { color: var(--color-red); background: #fee; }
.meld-tile.blue { color: var(--color-blue); background: #eef; }
.meld-tile.yellow { color: var(--color-yellow); background: #ffd; }
.meld-tile.black { color: var(--color-black); background: #eee; }
.meld-tile.wild { color: var(--color-wild); background: #f5f; }

.table-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-top: 12px;
    margin-bottom: 12px;
}

.remove-meld-btn {
    background: var(--color-danger);
    color: white;
    border: none;
    border-radius: 4px;
    padding: 6px 12px;
    cursor: pointer;
    font-size: 0.85rem;
    transition: opacity 0.2s;
}

.remove-meld-btn:hover {
    opacity: 0.8;
}

/* Add Meld Controls */
.add-meld-controls {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.add-meld-controls .input {
    flex: 1;
    min-width: 150px;
}

/* Capture Buttons Spacing */
.capture-buttons {
    margin-top: 12px;
}

/* Buttons */
.btn {
    padding: 12px 24px;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    min-height: 48px;
    touch-action: manipulation;
}

.btn:active {
    transform: scale(0.98);
}

.btn-primary {
    background: var(--color-primary);
    color: white;
    width: 100%;
}

.btn-primary:hover {
    background: #229954;
}

.btn-primary:disabled {
    background: var(--color-secondary);
    cursor: not-allowed;
}

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

.btn-secondary:hover {
    background: #7f8c8d;
}

/* Inputs */
.input {
    padding: 12px;
    border: 1px solid var(--color-border);
    border-radius: 8px;
    font-size: 1rem;
    min-height: 48px;
}

.input:focus {
    outline: none;
    border-color: var(--color-primary);
}

/* Solver Controls */
.controls-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 16px;
}

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

.control-group label {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--color-text);
}

/* Timer Widget */
.solver-controls {
    position: relative;
    text-align: center;
}

.solver-controls .btn-primary {
    display: inline-block;
    width: auto;
    min-width: 200px;
}

.timer-widget {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-left: 12px;
    vertical-align: middle;
    animation: fadeIn 0.3s ease-in;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: scale(0.8);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.timer-widget svg {
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
}

/* Results Display */
.results-display {
    padding: 16px;
    background: var(--color-bg);
    border-radius: 8px;
}

.result-success {
    color: var(--color-primary);
    font-weight: 600;
    margin-bottom: 16px;
}

.result-error {
    color: var(--color-danger);
    font-weight: 600;
    padding: 12px;
    background: #fee;
    border-radius: 8px;
}

.move-list {
    list-style: none;
}

.move-item {
    padding: 12px;
    background: var(--color-surface);
    border-radius: 8px;
    margin-bottom: 8px;
    border-left: 4px solid var(--color-primary);
}

.move-number {
    font-weight: 700;
    color: var(--color-primary);
    margin-right: 8px;
}

/* Raw moves section (collapsible) */
.raw-moves-section {
    margin-top: 20px;
    border-top: 1px solid var(--color-border);
    padding-top: 12px;
}

.raw-moves-section summary {
    cursor: pointer;
    color: var(--color-text-light);
    font-size: 0.9rem;
    padding: 8px 0;
    user-select: none;
}

.raw-moves-section summary:hover {
    color: var(--color-text);
}

.raw-moves-section .move-list {
    margin-top: 12px;
}

.raw-moves-section .move-item {
    border-left-color: var(--color-secondary);
    background: var(--color-bg);
}

.raw-moves-section .move-number {
    color: var(--color-secondary);
}

/* Save/Load Controls */
.save-load-controls {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.saved-states {
    margin-top: 16px;
}

.saved-state-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px;
    background: var(--color-bg);
    border-radius: 8px;
    margin-bottom: 8px;
}

.saved-state-name {
    font-weight: 600;
}

.saved-state-time {
    font-size: 0.85rem;
    color: var(--color-text-light);
}

.delete-saved-btn {
    background: var(--color-danger);
    color: white;
    border: none;
    border-radius: 4px;
    padding: 6px 12px;
    cursor: pointer;
    font-size: 0.85rem;
}

/* Dialog */
.dialog {
    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: 1000;
    padding: 16px;
}

.dialog-content {
    background: var(--color-surface);
    border-radius: 12px;
    padding: 24px;
    max-width: 500px;
    width: 100%;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
}

.dialog-content h3 {
    margin-bottom: 16px;
}

#saved-states-list {
    margin-bottom: 16px;
}

.saved-state-btn {
    width: 100%;
    text-align: left;
    padding: 12px;
    background: var(--color-bg);
    border: 1px solid var(--color-border);
    border-radius: 8px;
    cursor: pointer;
    margin-bottom: 8px;
    transition: all 0.2s;
}

.saved-state-btn:hover {
    background: var(--color-border);
}

/* Settings Modal */
.settings-group {
    margin-bottom: 20px;
}

.settings-group label {
    display: block;
    font-weight: 600;
    margin-bottom: 8px;
    color: var(--color-text);
}

.settings-group input,
.settings-group textarea {
    width: 100%;
    margin-bottom: 8px;
}

.settings-help {
    font-size: 0.85rem;
    color: var(--color-text-light);
    margin-top: 4px;
}

.settings-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.settings-actions .btn {
    flex: 1;
    min-width: 120px;
}

/* Footer */
footer {
    text-align: center;
    padding: 24px 0;
    color: var(--color-text-light);
    font-size: 0.9rem;
}

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

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

/* Loading State */
.loading {
    opacity: 0.6;
    pointer-events: none;
}

.loading::after {
    content: '...';
    animation: dots 1.5s steps(4, end) infinite;
}

@keyframes dots {
    0%, 20% { content: ''; }
    40% { content: '.'; }
    60% { content: '..'; }
    80%, 100% { content: '...'; }
}

/* Responsive Adjustments */
@media (max-width: 600px) {
    header h1 {
        font-size: 1.5rem;
    }

    header {
        padding-right: 50px;
    }

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

    .add-meld-controls {
        flex-direction: column;
    }

    .add-meld-controls .input,
    .add-meld-controls .btn {
        width: 100%;
    }

    .save-load-controls {
        flex-direction: column;
    }

    .save-load-controls .input,
    .save-load-controls .btn {
        width: 100%;
    }

    .hand-actions {
        flex-direction: column;
    }

    .hand-actions .btn {
        width: 100%;
    }

    .settings-actions {
        flex-direction: column;
    }

    .settings-actions .btn {
        width: 100%;
    }

    .toast-container {
        left: 10px;
        right: 10px;
        bottom: 10px;
        max-width: none;
    }

    .toast {
        width: 100%;
    }
}

/* Toast Notifications */
.toast-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-width: 400px;
}

.toast {
    background: var(--color-surface);
    border-radius: 8px;
    padding: 16px;
    box-shadow: var(--shadow-lg);
    display: flex;
    align-items: flex-start;
    gap: 12px;
    animation: slideIn 0.3s ease;
    border-left: 4px solid var(--color-primary);
}

.toast.success {
    border-left-color: var(--color-primary);
}

.toast.error {
    border-left-color: var(--color-danger);
}

.toast.info {
    border-left-color: var(--color-blue);
}

.toast-icon {
    font-size: 1.2rem;
    flex-shrink: 0;
}

.toast-content {
    flex: 1;
}

.toast-title {
    font-weight: 600;
    margin-bottom: 4px;
}

.toast-message {
    font-size: 0.9rem;
    color: var(--color-text-light);
}

.toast-close {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1.2rem;
    color: var(--color-text-light);
    padding: 0;
    flex-shrink: 0;
}

.toast-close:hover {
    color: var(--color-text);
}

@keyframes slideIn {
    from {
        transform: translateX(400px);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

/* Confirmation Queue */
.section-help {
    font-size: 0.9rem;
    color: var(--color-text-light);
    margin-bottom: 16px;
}

.confirmation-queue {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.confirmation-item {
    background: var(--color-bg);
    border-radius: 8px;
    padding: 16px;
    border: 2px solid var(--color-yellow);
}

.confirmation-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.confirmation-title {
    font-weight: 600;
    color: var(--color-text);
}

.confirmation-reason {
    font-size: 0.85rem;
    color: var(--color-text-light);
    font-style: italic;
    margin-bottom: 12px;
}

.confirmation-meld {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px;
    background: var(--color-surface);
    border-radius: 8px;
    margin-bottom: 12px;
}

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

.confirmation-actions .btn {
    flex: 1;
    padding: 8px 16px;
    min-height: auto;
}

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

.btn-approve:hover {
    background: #229954;
}

.btn-reject {
    background: var(--color-danger);
    color: white;
}

.btn-reject:hover {
    opacity: 0.9;
}

/* Logs Modal */
.logs-dialog {
    max-width: 600px;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
}

.logs-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.logs-header h3 {
    margin: 0;
}

.logs-controls {
    display: flex;
    align-items: center;
    gap: 12px;
}

.debug-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-size: 0.85rem;
    user-select: none;
}

.debug-toggle input[type="checkbox"] {
    display: none;
}

.toggle-switch {
    position: relative;
    display: inline-block;
    width: 40px;
    height: 20px;
    background: var(--color-secondary);
    border-radius: 10px;
    transition: background 0.3s;
    flex-shrink: 0;
}

.toggle-switch::after {
    content: '';
    position: absolute;
    top: 2px;
    left: 2px;
    width: 16px;
    height: 16px;
    background: white;
    border-radius: 50%;
    transition: transform 0.3s;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.debug-toggle input[type="checkbox"]:checked + .toggle-switch {
    background: var(--color-primary);
}

.debug-toggle input[type="checkbox"]:checked + .toggle-switch::after {
    transform: translateX(20px);
}

.btn-small {
    padding: 6px 12px;
    min-height: auto;
    font-size: 0.85rem;
}

.logs-container {
    flex: 1;
    min-height: 200px;
    max-height: 60vh;
    overflow-y: auto;
    background: var(--color-bg);
    border-radius: 8px;
    padding: 12px;
    font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Fira Code', monospace;
    font-size: 0.85rem;
}

.logs-actions {
    margin-top: 16px;
}

.log-entry {
    padding: 6px 8px;
    margin-bottom: 4px;
    border-radius: 4px;
    word-break: break-word;
    white-space: pre-wrap;
}

.log-entry.log {
    background: var(--color-surface);
    color: var(--color-text);
}

.log-entry.error {
    background: #fee;
    color: var(--color-danger);
}

.log-entry.warn {
    background: #ffd;
    color: #856404;
}

.log-entry .log-source {
    font-size: 0.75rem;
    color: var(--color-text-light);
    margin-right: 8px;
}

.log-entry .log-time {
    font-size: 0.75rem;
    color: var(--color-text-light);
    margin-right: 8px;
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
