:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --secondary-color: #64748b;
    --secondary-hover: #475569;
    --text-color: #1e293b;
    --bg-color: #ffffff;
    --border-color: #e2e8f0;
    --section-bg: #f8fafc;
    --shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);

    --dark-bg: #0f172a;
    --dark-text: #f1f5f9;
    --dark-section-bg: #1e293b;
    --dark-border: #334155;
    --dark-code-bg: #334155;
    --dark-log-bg: #1e293b;
    --dark-input-bg: #334155;
    --dark-input-text: #f1f5f9;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    line-height: 1.6;
    touch-action: manipulation; /* 提升移动端点击响应速度 */
}

button {
    padding: 0.375rem 0.75rem;
    border: 1px solid var(--primary-color);
    border-radius: 0.375rem;
    margin-bottom: 5px;
    white-space: nowrap;
    cursor: pointer;
    font-size: 0.9rem;
}

button:disabled {
    opacity: 0.65;
}

button.primary {
    color: #fff;
    background-color: var(--primary-color);
}

button.primary:hover {
    color: #fff;
    border-color: var(--primary-hover);
    background-color: var(--primary-hover);
}

button.secondary {
    color: #fff;
    background-color: var(--secondary-color);
    border-color: var(--secondary-color);
}

button.secondary:hover {
    color: #fff;
    border-color: var(--secondary-hover);
    background-color: var(--secondary-hover);
}

h1 {
    font-size: 2rem;
    font-weight: 600;
    text-align: center;
    margin: 2rem 0;
    color: var(--text-color);
}

h2 {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0 0 1rem 0;
    color: var(--text-color);
}

.section {
    background-color: var(--section-bg);
    padding: 1.5rem;
    margin-bottom: 1.5rem;
    border-radius: 8px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border-color);
}

code {
    padding: .2em .4em;
    margin: 0;
    font-size: 85%;
    background: #ccc;
    border-radius: 3px;
}

input[type=text],
input[type=number],
select {
    font-size: 0.9375rem;
    font-weight: 400;
    line-height: 1.5;
    color: var(--text-color);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 0.5rem 0.75rem;
    max-width: 100%;
    background-color: var(--bg-color);
    transition: border-color 0.2s, box-shadow 0.2s;
}

input[type=file] {
    font-size: 1rem;
    font-weight: 400;
    line-height: 1.5;
    color: #212529;
    max-width: 100%;
}

input::file-selector-button {
    font-size: 0.9rem;
    font-weight: 400;
    line-height: 1.5;
    border: 1px solid var(--primary-color);
    border-radius: 0.375rem;
    cursor: pointer;
}

select {
    padding: .3rem 2.25rem .3rem .75rem;
}

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

input[type=text]:disabled,
input[type=number]:disabled,
select:disabled {
    opacity: 0.65;
    cursor: not-allowed;
    background-color: #e9ecef;
    color: #6c757d;
}

label {
    margin-right: 4px;
    white-space: nowrap;
}

.main {
    width: 100%;
    max-width: 1000px;
    margin: 0 auto;
    padding: 0 1.5rem;
    background: var(--bg-color);
    font-size: 0.9375rem;
    font-weight: 400;
    line-height: 1.6;
}

.footer {
    display: flex;
    gap: 10px;
    font-size: 0.8rem;
    color: #666;
    flex-wrap: wrap;
    margin: 1rem 0;
}

.footer .links {
    display: flex;
    align-items: center;
}

.footer .links a {
    color: #666;
    text-decoration: none;
    position: relative;
    padding: 0 8px;
}

.footer .links a:first-child {
    padding-left: 0;
}

.footer .links a:not(:last-child)::after {
    content: "•";
    position: absolute;
    right: -4px;
    color: #999;
}

.footer a:hover {
    color: #0d6efd;
    text-decoration: underline;
}

.flex-container {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 8px;
}

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

.flex-group.right {
    margin-left: auto;
}

.debug {
    display: none !important;
}

.log-container {
    width: 100%;
    min-height: 100px;
    max-height: 300px;
    margin: 0;
    padding: 0.75rem;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    overflow-y: auto;
    overflow-x: hidden;
    font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace;
    font-size: 0.875rem;
    word-break: break-word;
}

.log-container .log-line {
    padding: 2px 0;
}

.log-container .time,
.log-container .action {
    display: inline-block;
    white-space: nowrap;
}

.log-container .time {
    color: #333;
    margin-right: 0.5em;
}

.log-container .action {
    color: #666;
    margin-right: 0.5em;
}

.canvas-container canvas {
    border: black solid 1px;
    max-width: 100%;
    height: auto;
    display: block;
    margin: 0 auto;
}

.canvas-container.crop-mode canvas {
    border: 2px dashed var(--primary-color);
    cursor: grab;
}

.status-bar {
    display: none;
    font-size: 85%;
    color: #666;
    margin-bottom: 10px;
    padding-bottom: 10px;
    border-bottom: 1px dotted #AAA;
}

.orientation-hint {
    font-size: 85%;
    color: #666;
    margin-bottom: 10px;
}

#orientationBadge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    border-radius: 999px;
    border: 1px solid var(--border-color);
    background: #f8fafc;
}

.toast-message {
    position: fixed;
    left: 50%;
    bottom: 24px;
    transform: translateX(-50%);
    z-index: 3000;
    max-width: min(92vw, 520px);
    padding: 0.55rem 0.9rem;
    border-radius: 8px;
    background: rgba(15, 23, 42, 0.92);
    color: #f8fafc;
    font-size: 0.9rem;
    box-shadow: var(--shadow);
    text-align: center;
}

canvas.text-placement-mode {
    border: 2px dashed var(--primary-color) !important;
    cursor: text !important;
}

.canvas-title {
    display: none;
    text-align: center;
    margin-bottom: 5px;
    color: var(--primary-color);
}

.canvas-tools {
    margin-top: 10px;
    justify-content: center;
}

.brush-tools,
.text-tools,
.todo-tools,
.schedule-tools,
.crop-tools {
    display: none;
}

.canvas-container.brush-mode .brush-tools,
.canvas-container.text-mode .brush-tools,
.canvas-container.eraser-mode .brush-tools,
.canvas-container.text-mode .text-tools,
.canvas-container.todo-mode .todo-tools,
.canvas-container.schedule-mode .schedule-tools,
.canvas-container.crop-mode .crop-tools {
    display: flex;
}

.canvas-container.crop-mode .tool-buttons,
.canvas-container.crop-mode .brush-tools,
.canvas-container.crop-mode .text-tools,
.canvas-container.crop-mode .todo-tools,
.canvas-container.crop-mode .schedule-tools {
    display: none;
}

.tool-button {
    width: 36px;
    height: 36px;
    font-size: 1.2rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-right: 5px;
    background-color: #f8f9fa;
    border: 1px solid #dee2e6;
    border-radius: 4px;
    padding: 0;
    cursor: pointer;
    transition: all 0.2s ease;
}

.tool-button:hover {
    background-color: #e9ecef;
    border-color: #ced4da;
}

.tool-button.active {
    background-color: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}

.tool-button.hide {
    display: none;
}

.device-modal {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.device-modal-content {
    width: 100%;
    max-width: 520px;
    background: var(--bg-color);
    color: var(--text-color);
    border-radius: 10px;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow);
    padding: 1rem;
}

.device-modal-header h3 {
    margin: 0 0 0.5rem 0;
}

.device-modal-hint {
    color: #64748b;
    margin-bottom: 0.75rem;
}

.device-modal-list {
    max-height: 300px;
    overflow-y: auto;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--section-bg);
    margin-bottom: 0.75rem;
    padding: 0.5rem;
}

.device-modal-item {
    width: 100%;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--bg-color);
    color: var(--text-color);
    padding: 0.6rem 0.7rem;
    margin-bottom: 0.5rem;
    text-align: left;
}

.device-modal-item:last-child {
    margin-bottom: 0;
}

.device-modal-item:hover {
    border-color: var(--primary-color);
    background: #eff6ff;
}

.device-modal-item .name {
    font-weight: 600;
    margin-right: 0.5rem;
}

.device-modal-item .recommended {
    display: inline-block;
    margin-right: 0.5rem;
    padding: 0.05rem 0.35rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 600;
    color: #ffffff;
    background: #16a34a;
}

.device-modal-item .rssi {
    color: #64748b;
    font-size: 0.85rem;
}

.device-modal-empty {
    color: #64748b;
    padding: 0.75rem;
    text-align: center;
}

.device-modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
}

@media (max-width: 768px) {
    .flex-container {
        flex-direction: column;
    }

    .flex-container.options .flex-group label {
        min-width: 80px;
    }

    .flex-group.right {
        margin-left: 0;
    }

    .canvas-tools.flex-container {
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
    }

    .canvas-tools .flex-group {
        justify-content: center;
        width: 100%;
    }

    .log-container {
        height: 150px;
        margin-top: 10px;
    }

    fieldset {
        padding: 8px;
    }

    button {
        width: auto;
    }

    input[type=text],
    input[type=number],
    select {
        max-width: 100%;
        margin-bottom: 5px;
    }
}

body.dark-mode .debug {
    display: flex !important;
}

body.dark-mode,
body.dark-mode .main {
    background-color: var(--dark-bg);
    color: var(--dark-text);
}

body.dark-mode .section {
    background-color: var(--dark-section-bg);
    border-color: var(--dark-border);
}

body.dark-mode h1,
body.dark-mode h2 {
    color: var(--dark-text);
}

body.dark-mode code {
    background: var(--dark-code-bg);
    color: #ff9800;
}

body.dark-mode input[type=text],
body.dark-mode input[type=number],
body.dark-mode select {
    background-color: var(--dark-input-bg);
    color: var(--dark-input-text);
    border-color: var(--dark-border);
}

body.dark-mode input[type=text]:disabled,
body.dark-mode input[type=number]:disabled,
body.dark-mode select:disabled {
    background-color: #1a1a1a;
    color: #666;
    border-color: #2a2a2a;
}

body.dark-mode input[type=file] {
    color: var(--dark-input-text);
    background-color: transparent;
    border-color: var(--dark-border);
}

body.dark-mode input[type=file]::file-selector-button {
    background-color: var(--dark-fieldset-bg);
    color: var(--dark-input-text);
    border-color: var(--dark-border);
}

body.dark-mode input[type=file]::file-selector-button:hover {
    background-color: #333;
    border-color: #444;
}

body.dark-mode .log-container {
    background: var(--dark-log-bg);
    border: 1px solid var(--dark-border);
}

body.dark-mode .log-container .time {
    color: #8bc34a;
}

body.dark-mode .log-container .action {
    color: #03a9f4;
}

body.dark-mode #orientationBadge {
    background: var(--dark-input-bg);
    border-color: var(--dark-border);
    color: var(--dark-text);
}


body.dark-mode .footer .links a:not(:last-child)::after {
    color: #666;
}

body.dark-mode .footer {
    color: #999;
}

body.dark-mode .footer a {
    color: #999;
}

body.dark-mode .footer a:hover {
    color: #64b5f6;
}

body.dark-mode .tool-button {
    background-color: var(--dark-input-bg);
    border-color: var(--dark-border);
    color: var(--dark-text);
}

body.dark-mode .tool-button:hover {
    background-color: #3a3a3a;
    border-color: #444;
}

body.dark-mode .tool-button.active {
    background-color: var(--primary-color);
    color: white;
    border-color: var(--primary-hover);
}

body.dark-mode .device-modal-content {
    background: var(--dark-section-bg);
    color: var(--dark-text);
    border-color: var(--dark-border);
}

body.dark-mode .device-modal-list {
    background: var(--dark-bg);
    border-color: var(--dark-border);
}

body.dark-mode .device-modal-item {
    background: var(--dark-input-bg);
    color: var(--dark-text);
    border-color: var(--dark-border);
}

body.dark-mode .device-modal-item:hover {
    background: #243447;
    border-color: var(--primary-color);
}

body.dark-mode .device-modal-item .recommended {
    background: #22c55e;
    color: #0f172a;
}