/* === Reset & Base === */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #f0f2f5;
    color: #2c3e50;
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
    /* Ensure content extends behind status bar on mobile */
    padding: 0;
    margin: 0;
}

/* === App Layout === */
#app {
    height: 100%;
    display: flex;
    flex-direction: column;
}

.screen {
    display: none;
    flex-direction: column;
    height: 100%;
    padding: 16px;
    padding-top: calc(16px + env(safe-area-inset-top, 0px));
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    overflow-y: auto;
}

.screen.active {
    display: flex;
}

/* === Buttons === */
.btn {
    background: #3498db;
    color: white;
    border: none;
    padding: 10px 18px;
    border-radius: 8px;
    font-size: 15px;
    cursor: pointer;
    touch-action: manipulation;
    user-select: none;
    transition: background 0.15s;
}

.btn:active {
    background: #2980b9;
}

.btn-sm {
    padding: 7px 14px;
    font-size: 13px;
}

.btn-icon {
    background: transparent;
    color: #3498db;
    padding: 6px 10px;
    font-size: 15px;
}

.btn-icon:active {
    background: rgba(52, 152, 219, 0.1);
}

.btn-file {
    display: inline-flex;
    align-items: center;
    cursor: pointer;
}

/* === Menu Screen === */
#menu-screen h1 {
    text-align: center;
    font-size: 24px;
    font-weight: 700;
}

#puzzle-list {
    flex: 1;
    overflow-y: auto;
}

.backup-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 8px 0 6px;
    flex-wrap: wrap;
    flex-shrink: 0;
}

.backup-status {
    font-size: 12px;
    color: #7f8c8d;
}

.puzzle-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    background: white;
    border-radius: 10px;
    margin-bottom: 8px;
    cursor: pointer;
    transition: background 0.15s;
    user-select: none;
    box-shadow: 0 1px 3px rgba(0,0,0,0.07);
}

.puzzle-item:active {
    background: #e8ecf1;
}

.puzzle-item.completed {
    opacity: 0.65;
}

.puzzle-status {
    font-size: 20px;
    flex-shrink: 0;
    width: 28px;
    text-align: center;
}

.puzzle-name {
    flex: 1;
    font-size: 16px;
    font-weight: 500;
}

.puzzle-size {
    font-size: 13px;
    color: #7f8c8d;
}

.btn-edit {
    background: none;
    border: none;
    font-size: 18px;
    cursor: pointer;
    padding: 4px 6px;
    opacity: 0.4;
    border-radius: 6px;
    flex-shrink: 0;
}

.btn-edit:active {
    opacity: 1;
    background: rgba(52, 152, 219, 0.1);
}

.btn-delete {
    background: none;
    border: none;
    font-size: 18px;
    cursor: pointer;
    padding: 4px 6px;
    opacity: 0.4;
    border-radius: 6px;
}

.btn-delete:active {
    opacity: 1;
    background: rgba(231, 76, 60, 0.1);
}

.btn-clear {
    background: none;
    border: 1px solid rgba(243, 156, 18, 0.45);
    color: #d35400;
    font-size: 12px;
    cursor: pointer;
    padding: 6px 10px;
    opacity: 0.85;
    border-radius: 999px;
    flex-shrink: 0;
}

.btn-clear:active {
    opacity: 1;
    background: rgba(243, 156, 18, 0.16);
}

.menu-buttons {
    display: flex;
    gap: 10px;
    padding-top: 12px;
    flex-shrink: 0;
}

.menu-buttons .btn {
    flex: 1;
}

/* === Library === */
.btn-refresh {
    margin-left: auto;
}

#library-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.library-status {
    font-size: 13px;
    color: #7f8c8d;
    margin-bottom: 10px;
}

.library-status.error {
    color: #c0392b;
}

.library-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    background: white;
    border-radius: 10px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.07);
}

.library-name {
    flex: 1;
    font-size: 16px;
    font-weight: 500;
}

.library-size {
    font-size: 13px;
    color: #7f8c8d;
}

.library-state {
    font-size: 12px;
    color: #27ae60;
}

.btn-download {
    background: #27ae60;
    flex-shrink: 0;
}

.btn-download:active {
    background: #219a52;
}

.btn-download:disabled {
    background: #95a5a6;
    cursor: default;
}

/* === Game Screen === */
.game-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
    flex-shrink: 0;
}

#game-title {
    display: none;
}

.game-header span {
    font-size: 18px;
    font-weight: 600;
    flex: 1;
}

.btn-back-arrow {
    font-weight: 800;
    font-size: 20px;
    padding: 6px 12px;
}

.game-header .btn-sm {
    flex-shrink: 0;
}

/* === Bottom Tool Bar === */
.game-bottom-bar {
    display: flex;
    justify-content: center;
    padding: 8px 0 10px;
    flex-shrink: 0;
    gap: 12px;
}

.tool-group {
    display: flex;
    gap: 0;
    border-radius: 10px;
    overflow: hidden;
    border: 2px solid #bdc3c7;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.btn-tool {
    background: white;
    color: #555;
    border: none;
    border-radius: 0;
    padding: 10px 16px;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-right: 1px solid #ddd;
    transition: background 0.12s, color 0.12s;
}

.btn-tool:last-child {
    border-right: none;
}

.btn-tool:active {
    background: #e8ecf1;
}

.btn-tool.active {
    background: #2c3e50;
    color: white;
}

.btn-tool.active:active {
    background: #34495e;
}

/* Победа: нижняя панель "Следующая" вместо инструментов */
.win-next-bar {
    display: flex;
    justify-content: center;
    width: min(520px, calc(100% - 32px));
}

.btn-next-puzzle {
    width: 100%;
}

.tool-icon {
    display: inline-block;
    width: 18px;
    height: 18px;
    line-height: 18px;
    text-align: center;
    font-size: 14px;
    font-weight: bold;
    border-radius: 3px;
}

.fill-icon {
    background: #2c3e50;
    border-radius: 3px;
}

.btn-tool.active .fill-icon {
    background: white;
}

.mark-icon {
    color: #c0392b;
    font-size: 16px;
    font-weight: bold;
}

.btn-tool.active .mark-icon {
    color: #f5b7b1;
}

.erase-icon {
    background: white;
    border: 2px solid #999;
    width: 14px;
    height: 14px;
}

.btn-tool.active .erase-icon {
    border-color: #ccc;
}

#game-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;      /* по горизонтали */
    justify-content: center;  /* по вертикали — по центру */
    overflow: auto;
    padding: 4px 4px 8px;
    min-height: 0;
}

/* === Nonogram Table === */
.nonogram-table {
    border-collapse: collapse;
    touch-action: none;
    user-select: none;
    box-shadow: 0 2px 10px rgba(0,0,0,0.08);
}

.nonogram-table td {
    width: var(--cell-size, 32px);
    height: var(--cell-size, 32px);
    min-width: var(--cell-size, 32px);
    min-height: var(--cell-size, 32px);
    text-align: center;
    vertical-align: middle;
    font-size: var(--clue-font, 13px);
    padding: 0;
    line-height: 1;
}

/* После решения используем отдельную таблицу без clue-блоков */
.nonogram-solved-only .game-cell {
    cursor: default;
    border: none !important;
    box-shadow: none !important;
    transition: none;
}

.nonogram-solved-only {
    box-shadow: none;
}

.nonogram-solved-only td {
    border: none !important;
}

.corner-cell {
    background: transparent;
}

.clue-cell {
    background: #e8ecf1;
    color: #2c3e50;
    font-weight: 500;
    transition: color 0.2s;
}

.clue-cell.clue-satisfied {
    color: #bdc3c7;
    text-decoration: line-through;
}

.col-clue {
    border-left: 1px solid #d5dbe0;
    border-top: 1px solid #d5dbe0;
}

.col-clue-last {
    border-bottom: 2px solid #34495e;
}

.row-clue {
    border-top: 1px solid #d5dbe0;
    border-left: 1px solid #d5dbe0;
}

.row-clue-last {
    border-right: 2px solid #34495e;
}

.game-cell {
    background: white;
    border: 1px solid #cbd5e0;
    cursor: pointer;
    transition: background 0.06s;
    font-weight: bold;
}

.game-cell.filled {
    background: #2c3e50;
}

.game-cell.marked {
    background: #fef9f9;
    color: #c0392b;
    font-size: calc(var(--cell-size, 32px) * 0.6);
    font-weight: bold;
    text-shadow: 0 0 1px rgba(192, 57, 43, 0.3);
}

.game-cell.error {
    box-shadow: inset 0 0 0 2px #e74c3c;
}

/* Thick borders every 5 cells */
.game-cell.thick-left {
    border-left: 2px solid #34495e;
}

.game-cell.thick-top {
    border-top: 2px solid #34495e;
}

.game-cell.thick-right {
    border-right: 2px solid #34495e;
}

.game-cell.thick-bottom {
    border-bottom: 2px solid #34495e;
}

.col-clue.thick-left {
    border-left: 2px solid #34495e;
}

.row-clue.thick-top {
    border-top: 2px solid #34495e;
}

/* === Win Banner (название + автор без фона, кнопка Следующая) === */
.win-banner {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    padding: 8px 0;
    margin-bottom: 8px;
    flex-shrink: 0;
    animation: banner-in 0.35s ease-out;
}

.win-banner.hidden {
    display: none;
}

.win-title-block {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.win-title-text {
    font-size: 22px;
    font-weight: 700;
    color: #2c3e50;
}

.win-author-text {
    font-size: 14px;
    color: #7f8c8d;
}

.btn-next-puzzle {
    background: #27ae60;
    padding: 10px 24px;
}

.btn-next-puzzle:active {
    background: #219a52;
}

@keyframes banner-in {
    from { opacity: 0; transform: translateY(-10px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* === Модальное окошко оценки (центр экрана) === */
.win-rating-modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.win-rating-modal.hidden {
    display: none;
}

.win-rating-modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
}

.win-rating-modal-content {
    position: relative;
    background: white;
    border-radius: 16px;
    padding: 28px 32px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    min-width: 260px;
}

.win-rating-modal-title {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    color: #2c3e50;
}

.rating-stars-modal {
    display: flex;
    gap: 8px;
    justify-content: center;
}

.rating-stars-modal .rating-star {
    font-size: 42px;
    width: 48px;
    height: 48px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: #f1c40f;
    transition: transform 0.15s, color 0.15s;
    user-select: none;
}

.rating-stars-modal .rating-star:hover {
    transform: scale(1.15);
    color: #f39c12;
}

.rating-stars-modal .rating-star.rated {
    color: #e67e22;
}

.btn-close-rating {
    background: #95a5a6;
    color: white;
    padding: 10px 24px;
    border: none;
    border-radius: 8px;
    font-size: 15px;
    cursor: pointer;
}

.btn-close-rating:active {
    background: #7f8c8d;
}

/* === Hint Button === */
.btn-hint {
    background: #f39c12;
}

.btn-hint:active {
    background: #e67e22;
}

/* Hint cell flash */
@keyframes hint-flash {
    0%   { box-shadow: inset 0 0 0 3px #f39c12; }
    50%  { box-shadow: inset 0 0 0 3px #f1c40f; }
    100% { box-shadow: none; }
}

.game-cell.hint-flash {
    animation: hint-flash 1.2s ease-out;
}

/* === Screen Headers === */
.screen-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 14px;
    flex-shrink: 0;
}

.screen-header span {
    font-size: 18px;
    font-weight: 600;
}

/* === Editor === */
.editor-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
    flex-wrap: wrap;
    flex-shrink: 0;
}

.editor-controls label {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 14px;
}

.editor-controls input[type="number"] {
    width: 55px;
    padding: 6px 8px;
    border: 1px solid #bdc3c7;
    border-radius: 6px;
    font-size: 14px;
    text-align: center;
}

.btn-from-image {
    cursor: pointer;
    background: #27ae60;
}

.btn-from-image:active {
    background: #219a52;
}

/* Image threshold bar */
#image-threshold-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 10px;
    padding: 10px 14px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.07);
    flex-wrap: wrap;
    flex-shrink: 0;
}

#image-threshold-bar label {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 14px;
}

#threshold-slider {
    width: 140px;
    cursor: pointer;
}

#threshold-value {
    font-size: 13px;
    color: #7f8c8d;
    min-width: 28px;
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 14px;
    cursor: pointer;
    user-select: none;
}

.checkbox-label input[type="checkbox"] {
    width: 16px;
    height: 16px;
    cursor: pointer;
}

#editor-container {
    display: flex;
    justify-content: center;
    margin-bottom: 16px;
    flex: 1;
    align-items: flex-start;
    overflow: auto;
}

.editor-table {
    border-collapse: collapse;
    touch-action: none;
    user-select: none;
    box-shadow: 0 2px 10px rgba(0,0,0,0.08);
}

.editor-cell {
    width: var(--cell-size, 32px);
    height: var(--cell-size, 32px);
    min-width: var(--cell-size, 32px);
    min-height: var(--cell-size, 32px);
    background: white;
    border: 1px solid #cbd5e0;
    cursor: pointer;
    transition: background 0.06s;
}

.editor-cell.filled {
    background: #2c3e50;
}

/* Thick borders every 5 cells in editor */
.editor-cell.thick-left {
    border-left: 2px solid #34495e;
}

.editor-cell.thick-top {
    border-top: 2px solid #34495e;
}

.editor-cell.thick-right {
    border-right: 2px solid #34495e;
}

.editor-cell.thick-bottom {
    border-bottom: 2px solid #34495e;
}

.editor-save {
    display: flex;
    gap: 10px;
    flex-shrink: 0;
}

.editor-save input[type="text"] {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid #bdc3c7;
    border-radius: 8px;
    font-size: 15px;
    min-width: 0;
}

.btn-export {
    background: #27ae60;
    flex-shrink: 0;
}

.btn-export:active {
    background: #219a52;
}

/* === Import === */
.import-body {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

#import-data {
    flex: 1;
    min-height: 100px;
    padding: 12px;
    border: 1px solid #bdc3c7;
    border-radius: 8px;
    font-family: monospace;
    font-size: 13px;
    resize: vertical;
}

.import-actions {
    display: flex;
    gap: 10px;
}

.import-actions .btn {
    flex: 1;
}

.import-help {
    background: white;
    padding: 14px;
    border-radius: 10px;
    font-size: 13px;
    line-height: 1.5;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.import-help p {
    margin: 8px 0 4px;
}

.import-help p:first-child {
    margin-top: 0;
}

.import-help code {
    display: block;
    background: #f0f2f5;
    padding: 8px 10px;
    border-radius: 6px;
    font-size: 12px;
    line-height: 1.5;
    word-break: break-all;
}

/* === Menu Header with User Bar === */
.menu-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
    flex-shrink: 0;
}

.menu-header h1 {
    margin: 0;
    font-size: 24px;
    font-weight: 700;
}

.user-bar {
    display: flex;
    align-items: center;
    gap: 8px;
}

.coins-display {
    font-size: 14px;
    font-weight: 600;
    color: #f39c12;
    background: rgba(243, 156, 18, 0.1);
    padding: 4px 10px;
    border-radius: 999px;
}

.btn-profile {
    background: #8e44ad;
}

.btn-profile:active {
    background: #7d3c98;
}

.menu-buttons-secondary {
    display: flex;
    gap: 10px;
    padding-top: 8px;
    flex-shrink: 0;
}

.menu-buttons-secondary .btn {
    flex: 1;
}

.btn-workshop {
    background: #e67e22;
}

.btn-workshop:active {
    background: #d35400;
}

.btn-leaderboard-nav {
    background: #2c3e50;
}

.btn-leaderboard-nav:active {
    background: #34495e;
}

/* === Game Timer === */
.game-timer {
    font-size: 14px;
    font-weight: 600;
    color: #7f8c8d;
    font-variant-numeric: tabular-nums;
    min-width: 40px;
}

/* === Rating stars (в модалке используются .rating-stars-modal) === */
.rating-stars {
    display: flex;
    gap: 8px;
}

.rating-star {
    cursor: pointer;
    color: #f1c40f;
    transition: transform 0.15s, color 0.15s;
    user-select: none;
}

/* === Workshop === */
.workshop-filters {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
    flex-wrap: wrap;
    flex-shrink: 0;
}

.workshop-tags-filter {
    flex: 1;
    min-width: 220px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.workshop-tags-title {
    font-size: 12px;
    color: #7f8c8d;
}

.workshop-hide-downloaded {
    flex-shrink: 0;
}

.workshop-sort-select {
    padding: 8px 10px;
    border: 1px solid #bdc3c7;
    border-radius: 8px;
    font-size: 14px;
    background: white;
}

.tag-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.tag-chip {
    border: 1px solid #bdc3c7;
    background: white;
    color: #2c3e50;
    padding: 6px 10px;
    border-radius: 999px;
    font-size: 12px;
    cursor: pointer;
    user-select: none;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.tag-chip:active {
    background: rgba(52, 152, 219, 0.08);
}

.tag-chip.selected {
    background: rgba(52, 152, 219, 0.12);
    border-color: rgba(52, 152, 219, 0.6);
    color: #2980b9;
}

#workshop-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.workshop-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    background: white;
    border-radius: 10px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.07);
}

.workshop-item-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.workshop-name {
    font-size: 16px;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.workshop-meta {
    font-size: 12px;
    color: #7f8c8d;
}

/* === Editor Tags Panel === */
.editor-tags-panel {
    background: white;
    border-radius: 10px;
    padding: 12px 14px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.07);
    margin-bottom: 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    flex-shrink: 0;
}

.editor-tags-title {
    font-size: 13px;
    color: #7f8c8d;
}

/* === Pagination === */
.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 12px 0;
}

.page-info {
    font-size: 14px;
    color: #7f8c8d;
}

/* === Profile === */
#profile-content {
    flex: 1;
    overflow-y: auto;
}

.profile-login {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    padding: 40px 20px;
    text-align: center;
}

.profile-login p {
    font-size: 15px;
    color: #555;
    line-height: 1.5;
}

.profile-info {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.profile-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    background: white;
    border-radius: 10px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.07);
    flex-wrap: wrap;
}

.profile-name-input {
    flex: 1;
    min-width: 120px;
    padding: 8px 12px;
    border: 1px solid #bdc3c7;
    border-radius: 8px;
    font-size: 14px;
}

.btn-daily-gift {
    background: #f39c12;
    width: 100%;
}

.btn-daily-gift:active {
    background: #e67e22;
}

/* === Leaderboard === */
#leaderboard-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.leaderboard-entry {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    background: white;
    border-radius: 10px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.07);
}

.lb-rank {
    font-size: 16px;
    font-weight: 700;
    color: #e67e22;
    min-width: 36px;
}

.lb-name {
    flex: 1;
    font-size: 15px;
    font-weight: 500;
}

.lb-score {
    font-size: 13px;
    color: #7f8c8d;
}

/* === My Publications === */
#my-publications-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.my-pub-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    background: white;
    border-radius: 10px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.07);
}

.my-pub-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.my-pub-name {
    font-size: 16px;
    font-weight: 500;
}

.my-pub-meta {
    font-size: 12px;
    color: #7f8c8d;
}

/* === Publish Button === */
.btn-publish {
    background: #e67e22;
    flex-shrink: 0;
}

.btn-publish:active {
    background: #d35400;
}

/* === Utility === */
.hidden {
    display: none !important;
}

/* === Профиль: вход и синхронизация === */
.profile-account {
    margin-top: 16px;
    padding-top: 14px;
    border-top: 1px solid rgba(255, 255, 255, 0.15);
}
.profile-account-title {
    font-weight: 700;
    margin-bottom: 6px;
}
.profile-account-hint {
    font-size: 13px;
    opacity: 0.75;
    margin: 0 0 10px;
}
.profile-account-buttons {
    gap: 8px;
    flex-wrap: wrap;
}
.profile-oauth-row {
    gap: 10px;
    flex-wrap: wrap;
    align-items: center;
    margin-top: 6px;
}
.btn-yandex {
    background: #fc3f1d;
    color: #fff;
}
